Johnny Mai
Johnny Mai

Reputation: 39

How to open files from a list

I figured out how to go through a directory and look for a certain type of file and add them to a list. Now I have a list with the names of the files. How do I open all of them? Here is what I have so far:

import os
import glob
import subprocess

os.chdir("/Users/blah/Desktop/Data")
reflist = glob.glob('*raw_ref.SDAT')
actlist = glob.glob('*raw_act.SDAT')

for i in reflist:
    os.popen("%r" %i)

for j in actlist:
    os.popen("%r" %j)

P.S. I'm on a Mac

Upvotes: 2

Views: 4234

Answers (3)

NHL
NHL

Reputation: 11

@Brian is right saying "I would recommend having as few files open simultaneously as possible." However this depends what you want to do. If you need several open files for reading, you can try this to make sure files are closed at the end:

# I don't know MAC path names.
filenames = ['/path/to/file', 'next/file', 'and/so/on']
# here the file descriptors are stored:
fds = []
try:
    for fn in filenames:
        # optional: forgive open errors and process the accessible files
        #try:
            fds.append(open(fn))
        #except IOError:
        #    pass

    # here you can read from files and do stuff, e.g. compare lines
    current_lines = [fd.readline() for fd in fds]
    # more code

finally:
    for fd in fds:
        fd.close()

Upvotes: 0

Brian
Brian

Reputation: 3131

I would recommend having as few files open simultaneously as possible.

for file in reflist:
    with open(file) as f:
        pass # do stuff with f
    # when with block ends, f is closed

for file in actlist:
    with open(file) as f:
        pass # do stuff with f
    # when with block ends, f is closed

If you, for some reason, need all the files open simultaneously (which I find unlikely), then go with NPE's solution.

Keep in mind that when you don't use a context manager for file I/O (like with is used here) that you will need to manually close the files when you are done.

Upvotes: 4

NPE
NPE

Reputation: 500437

ref_files = map(open, reflist)

Or, if you want finer control over the arguments to open():

ref_files = [open(filename, ...) for filename in reflist]

Upvotes: 4

Related Questions