[Python-Help] File Copying

Alex Martelli aleax at aleax.it
Mon Jun 24 13:10:01 EDT 2002


On Monday 24 June 2002 06:09 pm, Rajat Chopra wrote:
> How do you copy a file from one directory to another. I have looked all
> over and haven't found any documentation on it. Essentially, I want to copy
> a file from $SRC_DIRECTORY to $TARGET_DIRECTORY.

Python does not support variable names starting with $, so I can only 
guess at what you mean.  Maybe you want environment variables: you'll
find those in dictionary os.environ (obviously you need to import os).

You don't tell us what is the filename you want to copy; assuming you
have it in a string, you can join that string to each directory name with
os.path.join.  Finally, shutil.copyfile does the copy for you (you need to
import shutil).  Also look at shutil.copy, shutil.copy2, etc etc, depending
on exactly what you want to do.

Please don't post to both python-help AND python-list.  Choose one or the
other.  python-list has a lot more traffic; python-help is for those cases in
which you'd rather NOT let everybody in python-list know about your
problems.  Thanks for your cooperation on this issue.

Alex





More information about the Python-list mailing list