0

How to open files in a particular folder with randomly generated names? I have a folder named 2018 and the files within that folder are named randomly. I want to iterate through all of the files and open them up.

I will post three names of the files as an example but note that there are over a thousand files in this folder so it has to work on a large scale without any hard coding.

0a2ec2da-628d-417d-9520-b0889886e2ac_1.xml

00a6b260-951d-46b5-ab27-b2e8729e664d_1.xml

00a6b260-951d-46b5-ab27-b2e8729e664d_2.xml

Useless
  • 64,155
  • 6
  • 88
  • 132

2 Answers2

0

You're looking for os.walk().

In general, if you want to do something with files, it's worth glancing at the os, os.path, pathlib and other built-in modules. They're all documented.

You could also use glob expansion to expand "folder/*" into a list of all the filenames, but os.walk is probably better.

Useless
  • 64,155
  • 6
  • 88
  • 132
0

With os.listdir() or os.walk(), depending on whether you want to do it recursively or not.

You can go through the python doc

One you have list of files you can read it simply -

for file in files:
    with open(file, "r") as f:
        # perform file operations
Tabaene Haque
  • 576
  • 2
  • 10