How can I move and copy files using python?

Discussion in 'Python' started by Kay Lee, Oct 29, 2003.

  1. Kay Lee

    Kay Lee Guest

    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.
    Kay Lee, Oct 29, 2003
    #1
    1. Advertising

  2. 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
    Alex Martelli, Oct 30, 2003
    #2
    1. Advertising

  3. 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/lib/module-shutil.html


    Cheers
    --
    Martin Franklin <>
    Martin Franklin, Oct 30, 2003
    #3
  4. Kay Lee

    GrayGeek Guest

    <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
    GrayGeek, Oct 31, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kay Lee
    Replies:
    1
    Views:
    551
    Gary Herron
    Oct 30, 2003
  2. Replies:
    26
    Views:
    2,112
    Roland Pibinger
    Sep 1, 2006
  3. Eduardo78
    Replies:
    0
    Views:
    245
    Eduardo78
    Nov 3, 2005
  4. Andrew Tomazos
    Replies:
    2
    Views:
    611
    Nobody
    Dec 12, 2011
  5. Replies:
    1
    Views:
    315
    Daniel Pitts
    Feb 13, 2013
Loading...

Share This Page