Roslund
Roslund

Reputation: 23

How to use os.path.join() when finding many files

I want to find and open 500 Excel files so I can edit them. I was told it was possible to use the os.path.join() to get them simultaniously since they are in one folder. When I used:

file_location = os.path.join("C:\Users\A12345\Desktop\Folder_name","file_1.xlsm)

it worked but now I want to find and open all of them. Anyone have an idea how to do this in an easy way?

Upvotes: 1

Views: 814

Answers (1)

unwind
unwind

Reputation: 399793

All os.path.join() does is concatenate a path and a filename, i.e. it abstracts out the directory separation character for you. It has nothing to do with "getting" files.

You're going to have to use something like glob to get the filenames:

xlsms = glob.glob('C:\Users\A12345\Desktop\Folder_name\*.xlsm')

Then you can feed those to Excel I guess, using os.system():

cmd = "msexcel.exe %s" % " ".join(xlsms)
os.system(cmd)

Here I assume that Excel is available as msexcel.exe, and that it accepts hundreds of filenames on the command line.

Upvotes: 2

Related Questions