I've written a Python program which is supposed to do something straight forward and simple: read all the files in a C drive and create a spreadsheet containing just the files' names (I intend to make the program more complex later, so far I'm just in the first phase).
The problem I run into from time to time is that, while the program is scanning the C drive, runs into some files names (usually in Temp folders) that, for some reason, seem to be using a weird character that is not recognized by the program, and I get an error referring to the CP1252.
The problem is: my program worked fine in the computer I used first (my work computer), but it failed when I tried it home. My work computers uses Windows 7 32 bit, my home computer uses Windows 10 64 bit, but I've got the perception that the issue is not related to the OS.
import os
import csv
counter=0
wrkdir=os.getcwd()
logdir=wrkdir+"\\Logs"
resultfile=wrkdir+"/Logs/Report.csv"
with open(resultfile, 'w', newline='') as csvfile:
reportwriter=csv.writer(csvfile)
cdrive=("C:\\")
for dirName, subdirList, fileList in os.walk(cdrive):
path=str(dirName)
for fname in fileList:
name=str(fname)
fullname=path+"\\"+fname
reportwriter.writerow([fullname])
counter=counter+1
reportwriter.writerow(["Total number of files in C:/ ->",counter])
print ("Report created, "+str(counter)+" files detected.")
Does anyone know how can I get the program to work in any computer (at least, any computer running Windows) without a problem?
The error I get tend to look like this:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 224-225: character maps to