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 
34 
35 def createUniDir():
36  # Remove directory if it already exists
37  if os.path.exists(fullPath):
38  removeUniDir()
39  try:
40  # Make sure we are in the correct directory root
41  os.chdir(tempDir)
42  print 'Working Directory:\n ' + os.getcwd()
43  except IOError:
44  print '!FAIL! Could not set working directory!\n'
45  else:
46  print 'Moved to Temp folder:\n ' + os.getcwd()
47 
48  try:
49  # Make our directory
50  os.mkdir(fullPath)
51  except IOError:
52  print 'FAIL! Could not create unicode directory:\n' + fullPath
53  else:
54  print 'Created unicode directory:\n' + fullPath
55 
56 # Function to remove Unicode directory
57 
58 
59 def removeUniDir():
60  # Check if the directory exists
61  if os.path.exists(fullPath):
62  try:
63  # Change to our working folder to be safe
64  os.chdir(tempDir)
65  print 'Working Directory:\n ' + os.getcwd()
66  except IOError:
67  print '!FAIL! Directory does not exist!\n'
68  else:
69  # Since we know we are in our working folder, remove the Unicode
70  # directory created my createDir()
71  shutil.rmtree(uniDir)
72  print 'Removed unicode directory:\n' + fullPath
73 
74 # Function to create Unicode file in working directory
75 
76 
77 def openFile():
78  # Change to our working folder to be safe
79  os.chdir(tempDir)
80  # Set up our file and set it's encoding to UTF-8
81  with codecs.open(fname, encoding='utf-8', mode='w+') as f:
82  # Write to our file (this could be done as a try)
83  f.write(textStr + uniTextStr)
84  print 'Finished writing file to ' + fname
85  # Close our file
86  f.close()
87 
88 # Function to create Unicode file in Unicode directory
89 
90 
91 def openFileInUniDir():
92  # Change to our working folder to be safe
93  os.chdir(fullPath)
94  # Set up our file and set it's encoding to UTF-8
95  with codecs.open(fname, encoding='utf-8', mode='w+') as f:
96  # Write to our file (this could be done as a try)
97  f.write(textStr + uniTextStr)
98  print 'Finished writing file to ' + fullPath + fname
99  # Close our file
100  f.close()
101 
102 # Function to remove Unicode file
103 
104 
105 def removeUniFile():
106  # Change to our working folder to be safe
107  os.chdir(tempDir)
108  # Check if the file exists
109  if os.path.exists(tempDir + fname):
110  print 'File ' + fname + ' exists and will be removed!'
111  try:
112  # Remove our file
113  os.remove(tempDir + fname)
114  except IOError:
115  print '!FAIL! - File not deleted'
116  else:
117  print 'File Removed.'
118 
119 # Create some setup stats for output
120 stats = unicode('Setup:\n' + 'Current directory: ' + currentDir +
121  '\nOutput filename: ' + uniFile + '\nFile contents: ' + textStr + uniTextStr)
122 # Output stats
123 print stats
124 
125 # Run our functions
126 openFile()
127 createUniDir()
128 openFileInUniDir()
129 # Comment these out to leave written files and created directory
130 # to visually verify files and files content
131 removeUniDir()
132 removeUniFile()