demoUniCodeIO.py

demoUniCodeIO.py
1 # Import our modules
2 import MaxPlus
3 import tempfile
4 import os
5 import codecs
6 import shutil
7 
8 # Set our variables
9 #
10 # Strings for the file content
11 textStr = 'Text String: Hello!\n'
12 uniTextStr = u'Unicode String: 女時代'
13 #
14 # Get the current working folder
15 currentDir = os.getcwd()
16 #
17 # Create Unicode directory name
18 uniDir = u'時'
19 #
20 # Set our user folder to the user temp folder
21 tempDir = tempfile.gettempdir()
22 #
23 # Create Unicode file name
24 uniFile = u'MàxPɭüѕ.txt'
25 #
26 # Set our temp folder plus the Unicode directory
27 fullPath = tempDir + '\\' + uniDir
28 #
29 # Set our filename variable
30 fname = uniFile
31 
32 # Function to create Unicode directory
33 def createUniDir():
34  # Remove directory if it already exists
35  if os.path.exists(fullPath):
36  removeUniDir()
37  try:
38  # Make sure we are in the correct directory root
39  os.chdir(tempDir)
40  print 'Working Directory:\n ' + os.getcwd()
41  except IOError:
42  print '!FAIL! Could not set working directory!\n'
43  else:
44  print 'Moved to Temp folder:\n ' + os.getcwd()
45 
46  try:
47  # Make our directory
48  os.mkdir(fullPath)
49  except IOError:
50  print 'FAIL! Could not create unicode directory:\n' + fullPath
51  else:
52  print 'Created unicode directory:\n' + fullPath
53 
54 # Function to remove Unicode directory
55 def removeUniDir():
56  # Check if the directory exists
57  if os.path.exists(fullPath):
58  try:
59  # Change to our working folder to be safe
60  os.chdir(tempDir)
61  print 'Working Directory:\n ' + os.getcwd()
62  except IOError:
63  print '!FAIL! Directory does not exist!\n'
64  else:
65  # Since we know we are in our working folder, remove the Unicode directory created my createDir()
66  shutil.rmtree(uniDir)
67  print 'Removed unicode directory:\n' + fullPath
68 
69  # Function to create Unicode file in working directory
70 def openFile():
71  # Change to our working folder to be safe
72  os.chdir(tempDir)
73  # Set up our file and set it's encoding to UTF-8
74  with codecs.open(fname, encoding='utf-8', mode='w+') as f:
75  # Write to our file (this could be done as a try)
76  f.write(textStr + uniTextStr)
77  print 'Finished writing file to ' + fname
78  # Close our file
79  f.close()
80 
81 # Function to create Unicode file in Unicode directory
82 def openFileInUniDir():
83  # Change to our working folder to be safe
84  os.chdir(fullPath)
85  # Set up our file and set it's encoding to UTF-8
86  with codecs.open(fname, encoding='utf-8', mode='w+') as f:
87  # Write to our file (this could be done as a try)
88  f.write(textStr + uniTextStr)
89  print 'Finished writing file to ' + fullPath + fname
90  # Close our file
91  f.close()
92 
93 # Function to remove Unicode file
94 def removeUniFile():
95  # Change to our working folder to be safe
96  os.chdir(tempDir)
97  # Check if the file exists
98  if os.path.exists(tempDir + fname):
99  print 'File ' + fname + ' exists and will be removed!'
100  try:
101  # Remove our file
102  os.remove(tempDir + fname)
103  except IOError:
104  print '!FAIL! - File not deleted'
105  else:
106  print 'File Removed.'
107 
108 # Create some setup stats for output
109 stats = unicode('Setup:\n' + 'Current directory: ' + currentDir + '\nOutput filename: ' + uniFile + '\nFile contents: ' + textStr + uniTextStr)
110 # Output stats
111 print stats
112 
113 # Run our functions
114 openFile()
115 createUniDir()
116 openFileInUniDir()
117 # Comment these out to leave written files and created directory
118 # to visually verify files and files content
119 removeUniDir()
120 removeUniFile()