[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 13:38:50 CET 2010


> 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



More information about the Tutor mailing list