0

I'm new to Python so I'm not sure what this is called. How do I do something (attach to a variable, read it, etc.) to a file with the name of a file followed by a number (ex. Myfile1, Myfile2)?

Let's assume that there are a thousand files so it's not possible to type each number individually.

for n in range(0,1000):
    Myfile + str(n) = pd.read_csv(str(file(n)))

# Another example
for n in range(0,9):
    shape_201 + str(n) = (happiness201 + n).shape

Something like an asterisk (*) for a wildcard? Now that I have typed this out, regex comes to mind... Is that the best way to go?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Julian
  • 411
  • 4
  • 18
  • I'd say to follow this https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory to list your files and then use regex to filter files. One you got your filename list you can do anything with it. – Thom Mar 09 '19 at 16:52
  • 1
    So you will have upto `Myfile999`, 1000 variables!! Use some data structures, list for example. – Austin Mar 09 '19 at 16:52
  • No, not regex. You have actual file names. You don't want to use string formatting in assignment here. Just `df = pd.read_csv('some_file_name_{}.csv'.format(n))` – roganjosh Mar 09 '19 at 16:52
  • Use a dictionary. – Peter Wood Mar 09 '19 at 16:54
  • `file(n)` will fail. `file` expects a `str` filename, not an `int`. e.g. `file(str(n))`. Is your filename really `0`? – Peter Wood Mar 09 '19 at 16:55

2 Answers2

0

Given that you have 1000 files, whom all share the same basename, you could use the following code.

for i in range(1000):
  data = pd.read_csv("basename{i}.csv".format(i=i))
  //do something with data
Atle Kristiansen
  • 707
  • 7
  • 28
0

I'm not entirely sure what you're trying to do with the files once you've found them but I would build on other commenters' suggestions of avoiding regex and instead using glob.

For the purposes if this, I'm assuming your files are in this naming format:

  • File1.csv
  • File2.csv
  • ...

I'm partial to Pathlib because of its flexibility, so I would do something like this:

from pathlib import Path
baseDir = Path('/your/directory/here')
for fileName in baseDir.glob('File*.csv'):
    print(f'Found file {fileName}')
    # do whatever you want to do with each file, read, add to list, etc