We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,737 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Please Help!

Can anyone HELP me for my problem .. Im a newbie in python ! I want to create a script to rename files in a dir. that dir has 400 files .png .. startswith 001.png , 002.png to 400.png ! I want to rename them startswith 000.png to 399.png .. Please Help!!

7
Contributors
12
Replies
1 Month
Discussion Span
7 Months Ago
Last Updated
13
Views
crishein14
Newbie Poster
10 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Read the documentation of os module to find suitable command and use a loop and string formatting to do zero padded number:

print('%03i.txt' % 5)

Output:

005.txt
pyTony
pyMod
Moderator
6,301 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

If you're using linux, this may be handled more simply with a bash script.

hughesadam_87
Posting Whiz in Training
274 posts since May 2009
Reputation Points: 67
Solved Threads: 12
Skill Endorsements: 1

It`s simpel with python to,and this is a python forum.
@crishein14 save script in folder with files and run.

import os, glob

for numb,name in enumerate(glob.glob('*png')):
    os.rename(name, '{num:03d}.png'.format(num=numb))
snippsat
Posting Shark
956 posts since Aug 2008
Reputation Points: 482
Solved Threads: 344
Skill Endorsements: 8

Meister snippsat's solution is easy, but be careful, it will rename any .png file in the directory. You might want to make sure that the name starts with an integer at least.

Lardmeister
Posting Virtuoso
1,938 posts since Mar 2007
Reputation Points: 465
Solved Threads: 72
Skill Endorsements: 5

im only using pys60 .. Symbian ! Python says no module named glob :(

crishein14
Newbie Poster
10 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Try.

import os

for numb,name in enumerate(os.listdir(".")):
    if name.endswith(".png"):
        os.rename(name, '{num:03d}.png'.format(num=numb))

Or.
http://people.csail.mit.edu/kapu/symbian/python_old.html

glob, fnmatch
Standard Python modules that are not part of PyS60 distribution, with slight modifications
so they work with PyS60.
Glob allows directory listing with wild cards, fnmatch is used by glob. Used by sync, btconsole.

snippsat
Posting Shark
956 posts since Aug 2008
Reputation Points: 482
Solved Threads: 344
Skill Endorsements: 8

I think pyS60 use python 2.5.4,then you have to use old string formatting like this.
You should of course mention in first post that you use pyS60.

import os

for numb,name in enumerate(os.listdir(".")):
    if name.endswith(".png"):
        os.rename(name, '%03i.png' % numb)
snippsat
Posting Shark
956 posts since Aug 2008
Reputation Points: 482
Solved Threads: 344
Skill Endorsements: 8

thankyou so much for all of you .. im noob in python ! i want to learn more in python .. sorry for my bad english :)

crishein14
Newbie Poster
10 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

i try to run the code above .. python say's
for numb,name in enumerate(os.listdir("e:Py")):
NameError: name 'enumerate' is not defined

crishein14
Newbie Poster
10 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Implement it yourself

from __future__ import generators # does your python need this ?

def enumerate(iterable, start=0):
    i = start - 1
    for item in iterable:
        i += 1
        yield (i, item)
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

You should uppgarde your pyS60 version if enumerate() not work.
enumerate() was new in python 2.3 in 2003.
It`s difficult to write/learn python if you cannot use features implemented last 10 years.

snippsat
Posting Shark
956 posts since Aug 2008
Reputation Points: 482
Solved Threads: 344
Skill Endorsements: 8

the most widely used pys60 is v1.45 and is a port of python 2.2.2. the pys60 v1.9.7 that is a python 2.5 port that is less used.
for pys60 there are many modules for manipulating files and dirs, as an example I can suggest the pow_light fm module or fman module.there is also a shutil module for pys60.

M.S.
Junior Poster
117 posts since Jul 2011
Reputation Points: 64
Solved Threads: 13
Skill Endorsements: 2

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1327 seconds using 2.53MB