473,516 Members | 3,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I move and copy files using python?

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
3 86457
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2233
by: Kay Lee | last post by:
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.
2
2171
by: Sez | last post by:
Hi, I'm not a programmer. I start working as text miner and as a first task I have given 1000 dirty files that needs to be cleaned before classification tasks. I have been told python is the best tool for this job. Each file's structure as below: Comments: This is article 1965 obtained from the website
6
15971
by: Anand | last post by:
Hello, Can I get some help on how to read the excel files using python? from win32com.client import Dispatch xlApp = Dispatch("Excel.Application") xlWb = xlApp.Workbooks.Open("Read.xls") xlSht = xlWb.WorkSheets(1) But sadly, I am unable to proceed further about how to read the cells of the
14
6415
by: **--> That Guy Downstairs | last post by:
What files are needed to be #included to be able to copy files to a new directory and be portable? ie. use it in Unix (SGI and Linux) or Windows 2000. #ifdefs Ok. using dirent.h on SGI, but it's not working on Windows w/ VS6.0. Thanks.
1
5770
by: Shankar | last post by:
Hello, I am trying to run compiled Python files (*.pyc and *.pyo) using Python C API. I am using the method PyRun_FileFlags() for this purpose. The code snippet is as follows:- PyCompilerFlags myFlags;
13
1062
by: Avi | last post by:
Hi, Is there a UNIX C system command that will let me copy a file? I am looking for something similar to "cp" that can be called within a C program. I know of the "link" system call but this command will set a the second file as a link to the first file rather than an independent copy of the first file. (Windows has the CopyFile command...
1
1484
by: jazzy121 | last post by:
i want to copy a file usind default share on to the other pc which is also on lan and in same domain as well. the user from wich have logged on have full permissions. but when i try to copy the file it gives an error access denied ANy idea how to fix it ?
3
17714
by: SJM | last post by:
Howdy, hope someone can help me out with this. I want to run a job each night that copies files from one server to another. I cant even get a simple copy one file from one directory to another, statement to work. When I try : @cmd 'copy c:\temp\file1.txt c:\backups\file1.txt' master.dbo.xp_cmdshell @cmd I get the msg: 'c' is not...
1
3597
by: CORVID6116 | last post by:
Hi Guys, I can read and write .XLS files using Python. But now i need to write an XLS file with conditional formatting on some of the cells. Any help would be appreciated. Thanls
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7548
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.