By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

Renaming, Moving, Deleting all at one time in python

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import shutil
  3. import glob 
  4. import pyodbc
  5. import os.path
  7. #Counts the files
  8. def filecount(dir_name):
  9.     dir_name = 'D:\Applications\Prod\IMP\Software'
  10.     return len([f for f in os.listdir(dir_name) if os.path.isfile(f)])
  12. filecount()
  16. #Renaming
  17. while (f > 0):
  18.     def main():
  19.         d = 'D:\Applications\Prod\IMP\Software'
  20.         file = glob.glob('*.CSV')
  21.         for file in os.listdir(d):
  22.             title = 'Dialer_Import_ABC'
  23.             if file.endswith(".csv"):
  24.                 os.rename(file,title+".csv")
  26. main()
  28. #Moving the files
  30. def flytt():
  31.     destination = '\\sesrv413\f$\BulkInsert\Folder'
  32.     source = 'D:\Applications\Prod\IMP\Software'
  33.     file = 'D:\Applications\Prod\IMP\Software'\Dialer_Import_ABC.csv'
  34.     if file('D:\Applications\Prod\IMP\Software'\Dialer_Import_ABC.csv'):
  35.         shutil.move(destination, source)
  37. flytt()
  39. #Kör SP #UID=se.dialog.inv;PWD=Ajax123' vet ej om det behövs
  40. def SP():
  41. cnxn = pyodbc.connect("DRIVER={SQL Server};SERVER=sesrv413;DATABASE=Maintenance") 
  42.     cursor = cnxn.cursor()
  43.     cursor.execute("exec maintenance.dbo.PD_ABC_SP")
  45. SP()
  47. # Removing files
  49. def bort():
  50. myfile ="\\sesrv413\f$\BulkInsert\Folder\Dialer_Import_ABC.csv"
  51.     if os.path.isfile(myfile):
  52.         os.remove(myfile)
  54. bort()
Nov 10 '14 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 2.5K+
P: 2,851
More arguments error:
Expand|Select|Wrap|Line Numbers
  1. #Counts the files
  2. def filecount(dir_name):
  3.     dir_name = 'D:\Applications\Prod\IMP\Software'
  4.     return len([f for f in os.listdir(dir_name) if os.path.isfile(f)])
  6. filecount()
Expand|Select|Wrap|Line Numbers
  1. #Counts the files
  2. def filecount(dir_name):
  3.     return len([f for f in os.listdir(dir_name) if os.path.isfile(f)])
  5. 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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 10 '14 #2

P: 2
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
Nov 11 '14 #3

Expert Mod 2.5K+
P: 2,851
For renaming, a simple for loop would work. Possibly:
Expand|Select|Wrap|Line Numbers
  1. title = 'Dialer_Import_ABC'
  2. for filename in os.listdir(dirname)
  3.     if filename.endswith(".csv"):
  4.         newfilename = os.path.join((dirname,
  5.                 "%s%s%s" % (os.path.splitext(filename)[0], title, ".csv")))
  6.         os.rename(os.path.join((dirname, filename), newfilename)
Nov 11 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.