-2

I have folder where I'm putting updated csv files every day. I'm trying to write a code which will convert csv to excel but whatever I do I have always same issue with : [Errno 2] No such file or directory: ... or if I do not use encoding and try to first read the file then I get another error message: 'utf-8' codec can't decode byte 0xa7 in position 14: invalid start byte

for filename in os.listdir(my_path):
    with open(filename, encoding='utf-8') as f:
        f = pd.read_csv(os.path.join(my_path, f))
for filename in os.listdir(my_path):
    filename = pd.read_csv(os.path.join(my_path, filename))

Any suggestion how can I fix it?

Hubert S
  • 77
  • 7
  • Can you open the file in a text editor? It sounds like the file may not be a valid csv file and 0xa7 is a bit of an odd character. It would be useful to see the full Traceback for your error in both cases. You may also want to print out the file path you're loading for debugging purposes: you are scanning the full directory, not restricting to .csv files so you may be picking up a file you're not expecting. Note listdir will return directories as well. – John M. Aug 08 '22 at 10:09

1 Answers1

0

The below code will look at the my_path directory (note that you need to have the trailing \) and creates the equivalent excel files with same name (abc.csv to abc.xlsx) and puts it in the local directory.

my_path = "D:\Folder1\SampleDir\\" ##Where your csv files are - all files there should be csv only
currdir = os.getcwd() ## My current working directory from where code is run
for filename in os.listdir(my_path):
    df = pd.read_csv(os.path.join(my_path, filename)) #Read each file
    df.to_excel(os.path.join(currdir, filename.split(".")[0]+".xlsx"), index=False) #Write to xlsx in locl dir
Redox
  • 9,321
  • 5
  • 9
  • 26
  • Unfortunately, I get error message: 'utf-8' codec can't decode byte 0xa7 in position 14: invalid start byte – Hubert S Aug 08 '22 at 10:36
  • Hi Hubert, this is probably due to the way you are creating the csv files. Check out this [link](https://stackoverflow.com/questions/18171739/unicodedecodeerror-when-reading-csv-file-in-pandas-with-python) and see if you can identify if changing the `encoding` will resolve the issue – Redox Aug 08 '22 at 11:04