How can I move and copy files using python?

K

Kay Lee

Hi,

I looked up os module to find out some method to move and copy files in
python,
but os doesn't support such methods.
Is there any way to move & copy files in python?
Thanks in adv.
 
A

Alex Martelli

Kay said:
I looked up os module to find out some method to move and copy files in
python,
but os doesn't support such methods.
Is there any way to move & copy files in python?

Look at module shutil in the standard library.


Alex
 
G

GrayGeek

<posted & mailed>

Alex said:
Look at module shutil in the standard library.


Alex

Here's a script I wrote as part of a BBS system using SuSE 6.4 replace a
WildCat BBS running on Win98.

# Program: bbs_distribute.py
# run from command line using: python bbs_distribte.py
# or by making a small excutable script that has the same
# line in it, like so:
# #!/bin/bash
# python bbs_distribute.py
#
import os
a = os.system('clear')
print 'Mounting /floppy'
try:
a = os.system('mount /floppy')
except SystemError, detail:
print 'Cannot mount floppy: ', detail
os.exit()
print "Now copying files to home directories"
# command here to create etinfiles.txt
try:
a = os.system('ls /floppy/*.001 > ~/etinfiles.txt')
except IOError, detail:
print 'Cannot create etinfiles.txt: ', detail
os.exit()
# now open file and read names into var el
try:
f = open('etinfiles.txt','r')
except IOError, detail:
print 'Cannot open etinfiles.txt: ', detail
os.exit()
try:
el = f.read()
except IOError, detail:
print 'Cannot read from etinfiles.txt: ', detail
f.close()
os.exit()
f.close()
a = 0
while a < len(el):
tmp = el[a:a+20]
fname = tmp[8:20]
acctname = fname[0:5]
print fname, acctname
b = os.system('cp /floppy/'+fname+' /home/storage/'+fname)
if os.path.exists('/home/'+acctname):
b = os.system('cp /floppy/'+fname+' /home/'+acctname+'/'+fname)
# b = os.system('chmod /home/'+acctname+'/'+fname, 444)
b = os.system('chown '+acctname+':users
/home/'+acctname+'/'+fname)
else:
b = os.system('cp /floppy/'+fname+' /home/badaccts/'+fname)
a = a + 21
b = os.system('umount /floppy')
b = os.system('rm etinfiles.txt')
#
# now look in the /home/badaccts subdir for any accounts
# that have been activated.
#
print "Now processing missing account files from /home/badaccts..."
try:
a=os.system('ls /home/badaccts/*.001 > badfiles.txt')
except IOError,detail:
print "Cannot create badfiles.txt: ",detail
os.exit()
# now open file and read names into var el
try:
f=open('badfiles.txt','r')
except IOError,detail:
print "Cannot open badfiles.txt: ",detail
os.exit()
try:
el = f.read()
except IOError,detail:
print "Cannot read badfiles.txt: ",detail
os.exit()
f.close()
a=0
while a < len(el):
tmp = el[a:a+27]
fname = tmp[15:27]
acctname = fname[0:5]
print "Trying to move: ",fname, acctname
if os.path.exists('/home/'+acctname):
b = os.system('cp /home/badaccts/'+fname,
'/home/'+acctname+'/'+fname)
b = os.system('chown '+acctname+':users
/home/'+acctname+'/'+fname)
a = a + 28
#command to delete 'badfiles.txt'
b = os.system('rm badfiles.txt')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top