473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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.

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import shutil
  3. import glob 
  4. import pyodbc
  5. import os.path
  6.  
  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)])
  11.  
  12. filecount()
  13.  
  14.  
  15.  
  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")
  25.  
  26. main()
  27.  
  28. #Moving the files
  29.  
  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)
  36.  
  37. flytt()
  38.  
  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")
  44.  
  45. SP()
  46.  
  47. # Removing files
  48.  
  49. def bort():
  50. myfile ="\\sesrv413\f$\BulkInsert\Folder\Dialer_Import_ABC.csv"
  51.     if os.path.isfile(myfile):
  52.         os.remove(myfile)
  53.  
  54. bort()
  55.  
Nov 10 '14 #1
3 1571
bvdet
2,851 Expert Mod 2GB
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)])
  5.  
  6. filecount()
SHOULD BE
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)])
  4.  
  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
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
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

1
by: Erez Mor | last post by:
dear all i'm using vb2005 and SSE. using the application's startup event, i'm copying the db file (.mdf) to it's location (a ram-drive). that seems to work fine (i only connect to it after a...
5
by: Dylan Parry | last post by:
Hi, I'm trying to upload some files via a form, and I have managed so far to save the files on the server in a temporary directory using: HttpPostedFile.SaveAs(string filename); This is a...
4
by: gamename | last post by:
Hi, I'm a recent convert from TCL. One of the more powerful aspects of TCL is the ability to rename a function at will (generally for testing purposes). Example from the tcl doc: rename...
6
by: prodsd | last post by:
Here is my challenge . I have a new syatem That when I save a Adobe Illustrator file to my desktop it is ok but when I try to move or delete the file I get a error key that is open ??? I never had...
1
by: x40 | last post by:
I try to learn python thru solving some interisting problem, found google trasure hunt, write first program ( but cant find whats wrong). # Unzip the archive, then process the resulting files to...
2
by: Blubaugh, David A. | last post by:
To All, I have done some additional research into the possibility of utilizing Python for hard real time development. I have seen on various websites where this has been discussed before on...
0
by: Hendrik van Rooyen | last post by:
"Blubaugh, David A." <dblub....elcan.comwrote: I am running some i/o in a thing called an eBox running Slackware Linux for which GPIO port we have made some I/O boards. This is a very weak...
0
by: Kurt Mueller | last post by:
Am 08.10.2008 um 06:59 schrieb Hendrik van Rooyen: OK, this is gives an impression of SPEED. In your application the REAL-TIME requirements are isolated and implemented in the custom...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.