0

I'm trying to get a column count in a 500 csv files.

reading them into python wasn't an issue but the issue rests on how Python read them

from my understanding D:/myfiles/folder/anotherfolder/1folder/2folder is the correct way to input your filepath.

My script read it in correctly:

import glob
import os 
import pandas as pd
mycsvdir = 'filepath'
csvfiles = glob.glob(os.path.join(mycsvdir, '*csv'))

except for one issue

with the code above

I was expecting all my files to be directed like this D:/myfiles/folder/anotherfolder/1folder/2folder but now it reads in like this D:/myfiles/folder/anotherfolder\1folder/2folder

I'm trying to a string replacement on this ''

but this method:

csvfiles.replace('\',''/')

is giving me this error:

AttributeError: 'list' object has no attribute 'replace'

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `csvfiles` is a list. You need to loop over it, performing the replacement on each element. – Barmar May 11 '23 at 21:52
  • 2
    Why do you think you need to do this? Windows understands both forward slash and backslash in pathnames, and there's no problem with mixing them in the same path. – Barmar May 11 '23 at 21:53
  • `os.path.join()` always uses backslash, since it's the canonical format. – Barmar May 11 '23 at 21:54
  • I also suspect that the \ may not be an actual backslash character, but rather the start of an escape sequence for a non-printable character, such as `'\t'` or `'\n'`. – Blckknght May 11 '23 at 21:55
  • Yes, you need to escape the backslash: `replace('\\', '/')` – Barmar May 11 '23 at 21:55
  • In the code you provided, the `glob.glob()` function is being used to find all the files in the directory mycsvdir that end with the .csv extension. The output of the function is a `list of strings`, each of which is the path to a matching file. – D.L May 11 '23 at 22:13
  • There is *not any actual problem* with the `os.path.join` result: both forward slashes and backslashes can be provided whenever a path is needed. `os.path.join` uses backslashes because that is the platform's "native" standard, but forward slashes are recommended in code because they are portable and easier to work with. See the first linked duplicate. – Karl Knechtel May 12 '23 at 04:44
  • As for the `.replace` call, the backslash needs to be escaped, but more importantly, the `glob` gives you a list of strings, not just a single string. If you want to do something to each string in the list, you cannot just do that thing to the list directly. I provided the other reference links for those two problems. – Karl Knechtel May 12 '23 at 04:45

1 Answers1

0

This should do the trick:

import glob
import os
mycsvdir = 'filepath'
csvfiles = glob.glob(os.path.join(mycsvdir, '*csv'))
csvfiles = [cf.replace('\\', '/') for cf in csvfiles]
snoopyjc
  • 621
  • 4
  • 11
  • You're welcome - please remember to vote up my answer and use the checkbox to accept it as "the answer" – snoopyjc May 12 '23 at 20:37