Creating a function for a directory

unknown unknown at unknown.com
Tue Nov 12 06:26:25 EST 2013


On Tue, 12 Nov 2013 11:24:02 +0100, Peter Otten wrote:

> 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()

that's what i get for posting on the fly without checking first but i 
think the principle holds.



More information about the Python-list mailing list