Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How can I move and copy files using python?

Reply
Thread Tools

How can I move and copy files using python?

 
 
Kay Lee
Guest
Posts: n/a
 
      10-29-2003
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.

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      10-30-2003
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

 
Reply With Quote
 
 
 
 
Martin Franklin
Guest
Posts: n/a
 
      10-30-2003
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 <(E-Mail Removed)>


 
Reply With Quote
 
GrayGeek
Guest
Posts: n/a
 
      10-31-2003
<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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Select, Cut, Copy and Move Files subhabangalore@gmail.com Java 1 02-13-2013 05:01 PM
Writing move constructors and move assignment Andrew Tomazos C++ 2 12-12-2011 01:45 PM
I am trying to move spaces to a weblistbox and when I move them... Eduardo78 ASP .Net Web Controls 0 11-03-2005 06:06 PM
How can I move or copy files using python? Kay Lee Python 1 10-30-2003 04:16 PM



Advertisments