Change python script to run on every file in directory

so I have a python script that takes a file name as an argument to a command and processes that file. However, since I have 263 files that need the same processing, I was wondering if it is possible to change the section of the command argument using the for loop to sequentially run all the files in the folder? Cheers sat

EDIT:

The code for the system argument is here:

try:
    opt_list, args = getopt.getopt(sys.argv[1:], 'r:vo:A:Cp:U:eM:')

except getopt.GetoptError, msg:
    print 'prepare_receptor4.py: %s' %msg
    usage()
    sys.exit(2)

where "r" is the name of the file to be processed, and the rest are optional arguments. I'm not sure how to change this with a for loop.

+5
source share
5 answers

, , , , , . Bash, :

for f in *; do python myscript.py $f; done

Python, , , , .

def process(filename):
    ...code goes here...

,

for f in os.listdir(folder):
    process(f)

folder script ( , ).

EDIT: , -r, args.

for f in args:
    process(f)

,

for d in args:
    for f in os.listdir(d):
        process(f)

, , -r,

for opt, arg in opt_list:
    if opt == '-r':
        process(arg)
+9

os.walk() , .

def traverse_and_touch(directory, touch):
  '''
  General function for traversing a local directory. Walks through
  the entire directory, and touches all files with a specified function.
  '''
  for root, dirs, files in os.walk(directory):
    for filename in files:
      touch(os.path.join(root, filename))
  return

, , , , , .

os.walk() .

+4

/, os.walk:

import os
for root, dirs, files in os.walk(dir):
   for fname in files:
       do_something(fname) 

getopt optparse. , os.path.abspath, .

current_file = "%s%s%s" % (os.path.abspath(root), os.path.sep, fname)
do_something(current_file)
+3

, . , .

+1

, "" , . "args". "-r", . os.walk() .. , , , .

If a program works with a list of paths, it is very easy to use it in different ways. For example, you can list one data file for testing. To process the directory, run "myprogram dir / *. Dat". To process the file tree, use backticks:

myprogram `find . -name "*.dat"`

Finally, you can do very cheap parallel processing. Sort of:

find . -name '*.dat' | xargs -P 5 myprogram

Five copies of your program run in parallel. No locking or plugs or threads or other timing is required.

(The above assumes you are on a system such as Linux / OSX.)

+1
source

All Articles