By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

How can I move and copy files using python?

P: n/a
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.

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Kay Lee wrote:
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

Jul 18 '05 #2

P: n/a
On Wed, 2003-10-29 at 22:39, Kay Lee wrote:
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.


shutil module should have everything you need

http://www.python.org/doc/current/li...le-shutil.html
Cheers
--
Martin Franklin <mf********@gatwick.westerngeco.slb.com>
Jul 18 '05 #3

P: n/a
<posted & mailed>

Alex Martelli wrote:
Kay Lee wrote:
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


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')

--

-
GrayGeek
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.