473,372 Members | 1,040 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,372 software developers and data experts.

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','new_name2']
list_files = listdir(getcwd())
filtre = re.compile("jpg$", re.IGNORECASE)
list_jpg = filter(filtre.search, 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.strip())
#let's rename :
i=0
while i <= len(list_jpg_strip):
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 3601
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','new_name2']
list_files = listdir(getcwd())
filtre = re.compile("jpg$", re.IGNORECASE)
list_jpg = filter(filtre.search, 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.strip())
#let's rename :
i=0
while i <= len(list_jpg_strip):
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(path_to, nom.strip())
list_jpg_strip.append(old_path)
..
..
..
for old_path, new_name in zip(list_jpg_strip, list_names):
new_path = os.path.join(path_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_strip)" and i should
have been "while i < len(list_jpg_strip)".

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
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
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...
0
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...
7
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...
0
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...
10
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...
3
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...
0
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

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.