472,784 Members | 1,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

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 86389
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
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
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...
6
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")...
14
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...
1
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:- ...
13
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...
1
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...
3
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,...
1
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
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.