Home > Mobile >  How to convert multiple .XLSX files in a diretory to CSV using pandas?
How to convert multiple .XLSX files in a diretory to CSV using pandas?

Time:02-24

I'm trying to convert the .xlsx files of a folder into CSV using pandas, but I get the following error:

Code:

import os
import pandas as pd

path = os.path.join(r'C:\Users\Sandy Gomes\Desktop\folder1\*.xlsx')
files = os.listdir(path)

Error:

OSError Traceback (most recent call last)
C:\Users\JUNGLE~1\AppData\Local\Temp/ipykernel_1692/3413042739.py in <module>
      1 path = os.path.join(r'C:\Users\Sandy Gomes\Desktop\folder1\*.xlsx')
----> 2 files = os.listdir(path)

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\Sandy Gomes\\Desktop\\folder1\\*.xlsx'

Can you help me?

CodePudding user response:

Use this code to get a list of all xlsx file in folder1:

import os
import glob

os.chdir(r"C:\Users\Sandy Gomes\Desktop\folder1") #Change directory to folder1
filenames = glob.glob("*.xlsx") #List of filenames

print(filenames)

OR

path = (r"C:\Users\Sandy Gomes\Desktop\folder1\")
filenames = glob.glob(path   "*.xlsx")

print(filenames)

CodePudding user response:

That's was great! Tks so much!

  • Related