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

Why am I getting this error?

P: n/a
I am attempting to write a script that will take a directory as an argument then go through said directory and rename files with a given extension (in this case .jpg)

here is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. fileDir=os.listdir("c:\\Users\\Nathan\\Desktop\\Cell Pics\\")
  3. count = 1
  4. for fn in fileDir:
  5.  
  6.     if fn[-4:] == '.jpg':
  7.         os.rename(fn,'%03i.jpg' % count)
  8.         count += 1
  9.  
now, when I replace the directory above with '.' and drop the script into the directory I want to affect it will do its job perfectly. However when I have the directory put into the parameter of os.listdir and attempt to run it I get the following error:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:/Python27/test2.py", line 7, in <module>
  3.     os.rename(fn,'%03i' % count)
  4. WindowsError: [Error 2] The system cannot find the file specified
  5.  
What am I doing wrong?

I am using windows atm but this script is for UNIX. (if that matters let me know) I am pretty new to scripting and python (first week ever programming) so please be descriptive if at all possible!

thanks in advance!
Oct 8 '10 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
Join the path and the file name.
Expand|Select|Wrap|Line Numbers
  1. os.path.join("c:\\Users\\Nathan\\Desktop\\Cell Pics\\", fn)
Oct 8 '10 #2

Post your reply

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