the program is suppose to write all files on the hard drive to a file. when i run it in idle it runs until it hits a specific file and then gives me an error: UnicodeEncodeError: 'charmap' codec can't encode character '\™' in position 143: character maps to < undefined >
#! python3
import os
nfile=open('c:\\users\\computer 6\\desktop\\HardDrive.txt','w')
for folder,subfolder,files in os.walk('c:\\'):
if len(files) != 0:
for i in range(len(files)):
nfile.write(os.path.join(folder,files[i])+'\n')
else:
continue
nfile.close()
print('Log complete.')
i'm guessing this is because the file contains a Spanish letter?
Specify an encoding that supports all Unicode characters. open
defaults to locale.getpreferredencoding()
. \™
is a trademark symbol and is not supported by your default encoding:
#! python3
import os
with open('c:\\users\\computer 6\\desktop\\HardDrive.txt','w',encoding='utf8') as nfile:
for folder,subfolders,files in os.walk('c:\\'):
for file in files:
nfile.write(os.path.join(folder,file) + '\n')
print('Log complete.')
EDIT: I see I've missed some points in the question and this answer isn't right indeed. So please forget the following (I don't delete it only in a "historical" purpose).
You're most probably right. Had the same problem with french characters, solved this way:
for f in next(os.walk(full_path))[2]:
##
# @todo The following line is a hack to avoid encoding errors,
# it would be better to find a way to avoid doing this
# encoding/decoding on each file.
f = f.encode('utf-8', 'replace').decode()
file_name, extension = os.path.splitext(f)
etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.