How can I move and copy files using python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kay Lee

    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.

  • Alex Martelli

    #2
    Re: How can I move and copy files using python?

    Kay Lee wrote:
    [color=blue]
    > 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?[/color]

    Look at module shutil in the standard library.


    Alex

    Comment

    • Martin Franklin

      #3
      Re: How can I move and copy files using python?

      On Wed, 2003-10-29 at 22:39, Kay Lee wrote:[color=blue]
      > 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.[/color]

      shutil module should have everything you need




      Cheers
      --
      Martin Franklin <mfranklin1@gat wick.westerngec o.slb.com>


      Comment

      • GrayGeek

        #4
        Re: How can I move and copy files using python?

        <posted & mailed>

        Alex Martelli wrote:
        [color=blue]
        > Kay Lee wrote:
        >[color=green]
        >> 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?[/color]
        >
        > Look at module shutil in the standard library.
        >
        >
        > Alex[/color]

        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.p y
        # 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('clea r')
        print 'Mounting /floppy'
        try:
        a = os.system('moun t /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('chmo d /home/'+acctname+'/'+fname, 444)
        b = os.system('chow n '+acctname+':us ers
        /home/'+acctname+'/'+fname)
        else:
        b = os.system('cp /floppy/'+fname+' /home/badaccts/'+fname)
        a = a + 21
        b = os.system('umou nt /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('badfile s.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('chow n '+acctname+':us ers
        /home/'+acctname+'/'+fname)
        a = a + 28
        #command to delete 'badfiles.txt'
        b = os.system('rm badfiles.txt')

        --

        -
        GrayGeek

        Comment

        Working...