Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python glob.glob returns empty list on Mac

Tags:

python-3.x

I have folder full with different csv files which I would like to read, I used glob.glob on Windows and my code worked fine. I switched recently to Mac and when I used the same code it return empty list (with Spyder):

Windows code

data = [pd.read_csv(filename) for filename in glob.glob('C:/Data/*.csv')]

Mac code

data = [pd.read_csv(filename) for filename in glob.glob('~/Documents/Data/*.csv')]

Even if I remove the Tilda it return empty list in Mac

like image 389
Ahmed Avatar asked Sep 05 '26 10:09

Ahmed


1 Answers

You can use os.path.expanduser and then pass the resulting path to the glob.glob:

In [16]: os.path.expanduser('~/Documents/Data/*.csv')
Out[16]: '/Users/soon/Documents/Data/*.csv'
like image 164
awesoon Avatar answered Sep 07 '26 01:09

awesoon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!