Renaming, Moving, Deleting all at one time in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sterlinkArcher
    New Member
    • Nov 2014
    • 2

    Renaming, Moving, Deleting all at one time in python

    Hey Guys!

    I'm new to python and I've got only basic knowledge in the programming language that's why I'm turning to you. I work with MS SQL and .NET framework only. But now I want to make a script that moves firstly.

    Counts the number of files in the directory.

    Second it loops by taking one file at the time.

    Doing the following steps.

    Takes 1 file renaming it --> Moving it to other folder.

    Executing stored procedure --> deleting the file as last step.

    So far this is my code,I'm getting two errors first is that it requires more arguments and second is that the file cannot be found.

    I've spent days on this script and getting really tired of it, so any help would be very appriciated.

    Code:
    import os
    import shutil
    import glob 
    import pyodbc
    import os.path
    
    #Counts the files
    def filecount(dir_name):
        dir_name = 'D:\Applications\Prod\IMP\Software'
        return len([f for f in os.listdir(dir_name) if os.path.isfile(f)])
    
    filecount()
    
    
    
    #Renaming
    while (f > 0):
        def main():
            d = 'D:\Applications\Prod\IMP\Software'
            file = glob.glob('*.CSV')
            for file in os.listdir(d):
                title = 'Dialer_Import_ABC'
                if file.endswith(".csv"):
                    os.rename(file,title+".csv")
            
    main()
    
    #Moving the files
    
    def flytt():
        destination = '\\sesrv413\f$\BulkInsert\Folder'
        source = 'D:\Applications\Prod\IMP\Software'
        file = 'D:\Applications\Prod\IMP\Software'\Dialer_Import_ABC.csv'
        if file('D:\Applications\Prod\IMP\Software'\Dialer_Import_ABC.csv'):
            shutil.move(destination, source)
    
    flytt()
    
    #Kör SP #UID=se.dialog.inv;PWD=Ajax123' vet ej om det behövs
    def SP():
    cnxn = pyodbc.connect("DRIVER={SQL Server};SERVER=sesrv413;DATABASE=Maintenance") 
        cursor = cnxn.cursor()
        cursor.execute("exec maintenance.dbo.PD_ABC_SP")
    
    SP()
    
    # Removing files
    
    def bort():
    myfile ="\\sesrv413\f$\BulkInsert\Folder\Dialer_Import_ABC.csv"
        if os.path.isfile(myfile):
            os.remove(myfile)
    
    bort()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    More arguments error:
    Code:
    #Counts the files
    def filecount(dir_name):
        dir_name = 'D:\Applications\Prod\IMP\Software'
        return len([f for f in os.listdir(dir_name) if os.path.isfile(f)])
     
    filecount()
    SHOULD BE
    Code:
    #Counts the files
    def filecount(dir_name):
        return len([f for f in os.listdir(dir_name) if os.path.isfile(f)])
     
    filecount('D:\Applications\Prod\IMP\Software')
    os.listdir(path ) returns a list of entries in the directory given by path. In order to rename the file, you must supply the full path which could be give by:
    Code:
    os.path.join(dirname, filename)
    You should not use built-in function names for identifiers. Your use of file as an identifier will mask the built-in function file.

    Comment

    • sterlinkArcher
      New Member
      • Nov 2014
      • 2

      #3
      Thank you very much for you answer! It was indeed helpful. I've got a follow up question regarding the counting number of files.

      Is it possible to loop it? With a while-loop while filecount > 0 and then start the rest of the script?

      Best Regards

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        For renaming, a simple for loop would work. Possibly:
        Code:
        title = 'Dialer_Import_ABC'
        for filename in os.listdir(dirname)
            if filename.endswith(".csv"):
                newfilename = os.path.join((dirname,
                        "%s%s%s" % (os.path.splitext(filename)[0], title, ".csv")))
                os.rename(os.path.join((dirname, filename), newfilename)

        Comment

        Working...