0

So I'm trying to iterate through a list of files that are within a subfolder named eachjpgfile and change the file from doc to the subfolder eachjpgfile mantaining the file's name but when I do this it adds the file to directory before eachjpgfile rather than keeping it in it. Looking at the code below, can you see why is it doing this and how can I keep it in the eachjpgfile directory?

Here is the code:

for eachjpgfile in filelist:
    os.chdir(eachjpgfile)
    newdirectorypath = os.curdir
    list_of_files = os.listdir(newdirectorypath)
    for eachfile in list_of_files:
         onlyfilename = os.path.splitext(eachfile)[0]
         if onlyfilename == 'doc':
            newjpgfilename = eachfile.replace(onlyfilename,eachjpgfile)
            os.rename(eachfile, newjpgfilename)
7
  • 2
    os.chdir(eachjpgfile) What? Commented May 14, 2012 at 23:11
  • 2
    There is quite a lot wrong with this. :) Commented May 14, 2012 at 23:12
  • 4
    This will be much clearer if you use os.walk instead of reinventing this particular kind of wheel... Commented May 14, 2012 at 23:12
  • That was my silly way of changing the current directory to a new directory called eachjpgfile Commented May 14, 2012 at 23:12
  • Yes, I'm sure there is a better way. I didn't add my zipfile code here but basically I'm extracting this doc file into a new directory with the same name as zip file and trying to rename this doc file also to that same name but keep it in it's original directory. Commented May 14, 2012 at 23:24

2 Answers 2

1

There is a lot of weird stuff going on in here, but I think the one that's causing your particular issue is using 'eachjpgfile' in 'eachfile.replace'.
From what I can tell, the 'eachjpgfile' you're passing in is a full-path, so you're replacing 'doc' in the filename with '/full/path/to/eachjpgfile', which puts it parallel to the 'eachjpgfile' directory regardless of your current working directory.

You could add a line to split the path/file names prior to the replace:

for eachjpgfile in filelist:
    os.chdir(eachjpgfile)
    newdirectorypath = os.curdir
    list_of_files = os.listdir(newdirectorypath)
    for eachfile in list_of_files:
         onlyfilename = os.path.splitext(eachfile)[0]
         if onlyfilename == 'doc':
            root, pathName= os.path.split(eachjpgfile) #split out dir name
            newjpgfilename = eachfile.replace(onlyfilename,pathName)
            os.rename(eachfile, newjpgfilename)

which is a very dirty fix for a very dirty script. :)

Sign up to request clarification or add additional context in comments.

1 Comment

That worked, thanks. Yes, it's very dirty but it does work now. I probably should try to clean it up but very much a beginner and I'm sure it shows.
1

try this:

import os

path = '.'
recursive = False   # do not descent into subdirs

for root,dirs,files in os.walk( path ) :
    for name in files :
        new_name = name.replace( 'aaa', 'bbb' )

        if name != new_name :
            print name, "->", new_name
            os.rename( os.path.join( root, name),
                   os.path.join( root, new_name ) )

    if not recursive :
        break

1 Comment

thanks for the answer. Using the os.walk is I'm sure a much better route here. I will consider this for cleaning up my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.