473,569 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rename multiple files using names in a text file

Hi,

I would like to rename files (jpg's ones) using a text file containing the
new names...
Below is the code that doesn't work :
*****
#!/usr/bin/python
#-*- coding: utf-8 -*-
from os import listdir, getcwd, rename
import re
list_names=['new_name1','ne w_name2']
list_files = listdir(getcwd( ))
filtre = re.compile("jpg $", re.IGNORECASE)
list_jpg = filter(filtre.s earch, list_files)
#strip all element of list list_jpg
list_jpg_strip=[]
for nom in list_jpg:
#print nom.strip()
list_jpg_strip. append(nom.stri p())
#let's rename :
i=0
while i <= len(list_jpg_st rip):
rename(list_jpg _strip[i],list_names[i])
i=i+1
****
The error message is :
File "ecm.py", line 17, in <module>
rename(list_jpg _strip[i],list_names[i])
OSError: [Errno 2] No such file or directory
and all files exists, I checked it hundred times ! :-s
Do you have a clue ?
Thanks a lot.
Rémi.
Sep 14 '07 #1
2 3617
rémi wrote:
Hi,

I would like to rename files (jpg's ones) using a text file containing the
new names...
Below is the code that doesn't work :
*****
#!/usr/bin/python
#-*- coding: utf-8 -*-
from os import listdir, getcwd, rename
import re
list_names=['new_name1','ne w_name2']
list_files = listdir(getcwd( ))
filtre = re.compile("jpg $", re.IGNORECASE)
list_jpg = filter(filtre.s earch, list_files)
#strip all element of list list_jpg
list_jpg_strip=[]
for nom in list_jpg:
#print nom.strip()
list_jpg_strip. append(nom.stri p())
#let's rename :
i=0
while i <= len(list_jpg_st rip):
rename(list_jpg _strip[i],list_names[i])
i=i+1
****
The error message is :
File "ecm.py", line 17, in <module>
rename(list_jpg _strip[i],list_names[i])
OSError: [Errno 2] No such file or directory
and all files exists, I checked it hundred times ! :-s
Do you have a clue ?
Thanks a lot.
Rémi.
Other than that your strip() is stripping off some whitespace that is
part of the name, I really can't see the problem either, but did you try
to add in the explicit path? E.g.:

path_to = getcwd()
list_files = listdir(path_to )
..
..
..
for nom in list_jpg:
old_path = os.path.join(pa th_to, nom.strip())
list_jpg_strip. append(old_path )
..
..
..
for old_path, new_name in zip(list_jpg_st rip, list_names):
new_path = os.path.join(pa th_to, new_name)
rename(old_path , new_path)

James
Sep 14 '07 #2
Le Fri, 14 Sep 2007 12:52:52 -0700, James Stroud a écrit:

[...]
Other than that your strip() is stripping off some whitespace that is
part of the name, I really can't see the problem either, but did you try
to add in the explicit path? E.g.:
actually, the problem was in "while i <= len(list_jpg_st rip)" and i should
have been "while i < len(list_jpg_st rip)".

Antoher problem was the name of jpg's files used for testing. A naughty
one was nammed "file.jpg " instead of "file.jpg". So maube, as you saif,
stripping is useless for list of jpg files.
Thanks a lot.
Rémi
Sep 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4446
by: Rothariger | last post by:
Hello.... i want to know if its posible to rename multiple files like windows does.. example: file zzzzzzz.doc file asdasd.doc file esfsefse.doc
1
5112
by: Susan | last post by:
Is it possible to use PHP with VBScript or JavaScript to upload multiple files. I'd like to automate the upload of a complete directory (not using FTP). I don't need a common form with several upload boxes. Thanks in advance, Susan
0
1999
by: Web learner | last post by:
Individual file download works fine. But when I try to download multiple files, for example using wildcards in the following code, I get error. How to solve it? System.Net.WebClient Client = new System.Net.WebClient(); Client.DownloadFile("ftp://123.123.456.0/*.RPT", @"C:\downloadedFiles\*.RPT"); Are there other possibilities??
7
12244
by: Pete | last post by:
I need to import multiple tab delimited files for which I do not have the formats. All files have column headers in the the line. The files must be dynamic. The only common value is that the files are text and use tabs to delimit the column data. I can not use the File/Import as people using the program do not have the necessary...
0
1120
by: hash4sp | last post by:
I have just started my career in programming. My Problem is reading a text file using VB.NET. The text contains the state names followed by respective city names. My task is to read the file and generate an html page for each state with ciites. File format is as follows: Andhra Pradesh Hyderabad Warangal
10
4802
by: Sebarry | last post by:
Hi, I'm using the code at http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/ to upload multiple files with a single file element. It works fine but for each file uploaded I want to store the contents of a text file which describes the file. I've tried to understand the code but I'm not a...
3
2872
by: Davo1977 | last post by:
Does anyone know a regular expression that will rename multiple files that have different extensions to have the same extension. For example, you could use this code when several text files exist in a directory but have slightly different extensions such as .txt,. TXT, or text. This expression should show how to rename them to all have .txt...
0
963
by: nilesh tayde | last post by:
Hi all i want to write same data in single text file multiple times using this code. for singel time it work fine. Dim lngRet As Long On Error GoTo fWriteRemoteReport_Err '''''''''
9
8137
by: Man4ish | last post by:
Hi, I am newbie to multi-threading. I am working on one pblm where i need to process 24 files (each size = 3 GB) and write the output onto multiple files(24). Each file takes around 1 hour to process. Is it possible to write data onto multiple files concurrently using multi-threading? Thanks in advance.
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.