2

I want to create a function that will iterate through a list to file names that I put in, and then when the conditions are met I want it to copy each subsequent file to a file named 'INPUT'.

Here is what I have so far:

file_list = [file1, file2, file3]
def queue():
    if condition1_met:
        for item in file_list:
            os.system('cp item INPUT')
        else:
            return queue()

However I just don't know what the best way is to iterate and then copy the file, since that os.system bit won't recognize 'item'.

4
  • Copying each item to INPUT means they each replace the previous version. Only the last such copy will remain. Commented Jul 23, 2013 at 14:40
  • Yes, that is what I want, however I do want to preserve the original file. Commented Jul 23, 2013 at 14:41
  • Why, are you using os.system() for this operation? Wouldn't handling file objects entirely in python suffice? Commented Jul 23, 2013 at 15:06
  • I don't think you want that else clause after the for. It will always run (because there is no break in the for loop) and so queue() will keep calling itself and copying files until python hits its recursion limit. Did you really want recusion in this function? Commented Jul 23, 2013 at 15:51

2 Answers 2

1

You have to replace:

os.system('cp item INPUT')

by:

os.system('cp %s INPUT' % item)

So the value of the item iterator is substituted in the string and being executed as an OS call.

Edited according comment:

Better is to use shutil:

shutil.copyfile(item, 'INPUT')

This way no substition is needed.

Sign up to request clarification or add additional context in comments.

2 Comments

Please use shutil for these operations.
@Jan-PhilipGehrcke Edited answer according your comment.
0

This code will copy each file to INPUT, overwriting INPUT each time:

import shutil

file_list = [file1, file2, file3]
def queue():
  if condition1_met:
    for item in file_list:
      shutil.copyfile(item, "INPUT")
    else:
      return queue()

If you want to append the content of the files to INPUT, then this should do the job:

python 2.6 version

import contextlib

file_list = [file1, file2, file3]
def queue():
  if condition1_met:
    for item in file_list:
      with contextlib.nested(open(item,"r"), open("INPUT","a+")) as (src,dst):
        dst.writelines(src.readlines())
    else:
      return queue()

python 2.7 version

file_list = [file1, file2, file3]
def queue():
  if condition1_met:
    for item in file_list:
      with open(item,"r"), open("INPUT","a+") as src,dst:
        dst.writelines(src.readlines())
    else:
      return queue()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.