1

windows 7, python 2.7.2

The following runs w/o error:

from subprocess import call f = open("file1","w") f.writelines("sigh") f.flush f.close call("copy file1 + file2 file3", shell=True) 

However, file3 only contains the contents of file2. Both file1 and file2 names are echoed as is normal for windows, but, file1 appears to be empty when the copy is called. It seems as if file1 has not been completely written and flushed. If file1 has been created separately, as opposed to within the same python file, the following runs as expected:

from subprocess import call call("copy file1 + file2 file3", shell=True) 

Sorry if python newbieness is to be blamed here. Many thx for any assist.

1 Answer 1

7

You're missing the parentheses:

f.flush() f.close() 

Your code is valid syntactically, but doesn't call the two functions.

A more Pythonic way to write that sequence is:

with open("file1","w") as f: f.write("sigh\n") # don't use writelines() for one lonely string call("copy file1 + file2 file3", shell=True) 

That'll automatically close f at the end of the with block (and the flush() is redundant anyway).

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.