Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python glob path issue [duplicate]

Tags:

python

glob

I have a question about using glob in python.

I want to know why the path is showing backslashes instead of a forward slash?

Example being C:/Users/Name/Desktop/Pythonfiles\excel.xlsx

My script is

import glob
excel_list = (glob.glob("C:/Users/Name/Desktop/Pythonfiles/*.xlsx"))

This is my output I'm getting:

['C:/Users/Name/Desktop/PythonFiles\\19282.xlsx', 'C:/Users/Name/Desktop/PythonFiles\\19557.xlsx', 'C:/Users/Name/Desktop/PythonFiles\\19667.xlsx', 'C:/Users/Name/Desktop/PythonFiles\\19742.xlsx', 'C:/Users/Name/Desktop/PythonFiles\\CEImport.xlsx']

Any help would be great thank you

like image 936
BPCAL Avatar asked Aug 12 '26 16:08

BPCAL


1 Answers

\ and / are interchangeable path separators, but if you wanted to normalize the paths so they are uniform, use os.path.normpath

excel_path = [os.path.normpath(i) for i in glob.glob("C:/Users/Name/Desktop/Pythonfiles/*.xlsx")]

#output
['C:\\Users\\Name\\Desktop\\PythonFiles\\19282.xlsx',
 'C:\\Users\\Name\\Desktop\\PythonFiles\\19557.xlsx',
 'C:\\Users\\Name\\Desktop\\PythonFiles\\19667.xlsx',
 'C:\\Users\\Name\\Desktop\\PythonFiles\\19742.xlsx',
 'C:\\Users\\Name\\Desktop\\PythonFiles\\CEImport.xlsx']

like image 154
dubbbdan Avatar answered Aug 14 '26 07:08

dubbbdan