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?
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)
Another way to get all the files from the folder is to use pathlib
:
from pathlib import Path
# Path.cwd() get the current working directory - this can be replaced with any directory
# .glob gets all the files of the specified type
# **/*.xlsx recursively searches all the folders for the given file type
files = list(Path(Path.cwd()).glob("**/*.xlsx"))
Here is an example of using pathlib
set python modules search paths
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.