[Tutor] How to copy file from a source, I do not know the source file name, Only path to the src directory I know

Evert Rol evert.rol at gmail.com
Tue Nov 9 14:47:48 CET 2010


> I was able to solve it
> by
> "
> dirList=os.listdir(src)
> for fname in dirList:
> 	print fname
> 	ffname = '/home/joseph/Projects/Training/Python/example/One/'+fname

tip: use os.path.join for concatenating paths. It's smart, and (in principle) OS-agnostic.

Also, if you use 'os.listdir(src)', why not use src again when assigning ffname?


Cheers,

  Evert

> 	print ffname
> 	#shutil.copy(ffname,dst2)
> 	shutil.move(ffname,dst3)
> "
> 
> 
> On Tue, Nov 9, 2010 at 4:38 PM, Evert Rol <evert.rol at gmail.com> wrote:
>>> Hi All
>>>   I am trying our "Python" .
>>>   My aim is to copy a file to a location and later removing the
>>> file.I had written a small script which works.It works only if I
>>> specify the source file name. I would like to make it work in a
>>> scenario in which I do not know the name of the source file(the source
>>> directory will have only one file at any time)
>> 
>> Have a look at the os module, specifically os.listdir()
>> The glob module could also be handy.
>> 
>> Cheers,
>> 
>>  Evert
>> 
>>> 
>>> I am posting the script which I have done,How to modify the script to
>>> copy the source file even if I do not know the name of the file
>>> my script is below
>>> 
>>> ###############################################
>>> #!/usr/bin/python
>>> 
>>> # This script aims to copy file from one directoy to anothe and later
>>> removing the files
>>> # This script works when I know the source file name, I want to this
>>> script to function even if I do not know the name of
>>> # the file in "/home/joseph/Projects/Training/Python/example/One/"
>>> 
>>> import shutil
>>> # "FromOne" is a file in /home/joseph/Projects/Training/Python/example/One/
>>> src = "/home/joseph/Projects/Training/Python/example/One/FromOne"
>>> #
>>> dst2 = "/home/joseph/Projects/Training/Python/example/Two/"
>>> dst3 = "/home/joseph/Projects/Training/Python/example/Three/"
>>> #
>>> 
>>> #
>>> #shutil.move(src, dst)
>>> shutil.copy(src,dst2)
>>> shutil.move(src,dst3)
>>> 
>>> ######################################
>>> 
>>> 
>>> 
>>> 
>>> --
>>> Thanks
>>> Joseph John
>>> http://www.oss101.com/
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>> 
>> 
> 
> 
> 
> -- 
> Thanks
> Joseph John
> http://www.oss101.com/



More information about the Tutor mailing list