Creating a function for a directory

Peter Otten __peter__ at web.de
Tue Nov 12 05:24:02 EST 2013


unknown wrote:

> On Mon, 11 Nov 2013 14:26:46 -0800, Matt wrote:
> 
>> So I want to take the file, "desktop/test.txt" and write it to
>> "desktop/newfolder/test.txt". I tried the below script, and it gave me:
>> "IOError: [Errno 2] No such file or directory: 'desktop/%s.txt'". Any
>> suggestions would be great.
>> 
>> 
>> 
>> def firstdev(file):
>> in_file = open("desktop/%s.txt") % file indata = in_file.read()
>> out_file = open("desktop/newfolder/%s.txt", 'w') % file
>> out_file.write(indata)
>> out_file.close()
>> in_file.close()
>> 
>> firstdev("test")
> 
> would it not be more efficient and less error prone to use the os module
> to copy the file rather than manually reading & re-writing it (unless of-
> course you intend to expand this in future to process the data first)?

Hmm, I don't see a suitable function in os -- but there is 

shutil.copy()




More information about the Python-list mailing list