Converting a Shell Script to run under Python

Dominic Mitchell Dom at myrddin.demon.co.uk
Tue Dec 7 02:07:39 EST 1999


"Austin Wilson" <wilson.austin.aj at bhp.com.au> writes:
> What is the command for copying file1 to file3 (eg equivalent to cp file1
> file3) and creating a soft link to a file (eg ln -s file2 file1)?

os.system("cp file1 file2")
os.system("ln -s file2 file1")

Well, that's an almost exact duplicate of what the shell does.  :-)
More seriously, try this:

# Copy a file.  You may like to use a couple of temp. vars.
open('file2').write(open('file1').read()

# Symlink a file.
os.symlink('file2', 'file1')

Note that the latter is dependent on being run on a Unix box.
Generally, python's system programming interface closely follows that
of C (except where it doesn't, so have a go at reading the manual
pages under sections 2 and 3, then try searching for that in the
python manual.

-Dom



More information about the Python-list mailing list