Re: [Tutor] conditional renaming folder and files in the tree
On 08/14/2017 09:18 AM, banda gunda wrote: > Dear Tutor, > > > I have made some progress! > > But not yet got the results. > > Attached is revised code. > > > Specifically, the problem in below: > > > for root, dirs, files in os.walk(".", topdown=False): > for name in files: > print(os.path.join(root, name)) > os.rename(path + name, path + name.replace("---", "changed")) > #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", > "changed") > files[name] = os.sep.join([dirpath, name]) > > print (files) > > for name in dirs: > print(os.path.join(root, name)) > > > .\---DAT1\---DAT3\---10.txt > > > --- > FileNotFoundError Traceback (most recent call last) > in () > 2 for name in files: > 3 print(os.path.join(root, name)) > > 4 os.rename(path + name, path + name.replace("---", "changed")) > 5 #os.rename(path + "\\"+ name, path + "\\"+ > name.replace("---", "changed") > 6 files[name] = os.sep.join([dirpath, name]) > > FileNotFoundError: [WinError 2] The system cannot find the file specified: > 'E://---10.txt' -> 'E://changed10.txt' Well, here are a few thoughts. (1) it will really help if your debugging print in line 3 makes the same path calculation as the command in line 4. You can see these don't match, which is a good hint you're getting something you don't expect. (2) where are you getting "path" from? It doesn't show in your code. Is there any reason why you don't just use the same calculation as in line 3? (3) for cautious programming, you can check if the file you are going to rename exists (os.path.exists() or os.path.isfile()) before trying to rename it, that would at least cause you not to take an exception and quit when you get something wrong. (4) you've still not fixed "dirpath" in line 6. Note the python documentation for the walk() function shows dirpath as the first element of the result triple, but you've called it "root" in your code, so this is probably what you meant. (5) why are you printing the whole list of files in line 7? That means you will show the files list each time you iterate through the list. (6) it's usually not a great idea to modify an interable while you're iterating over it (line 6). (7) you indicate you want to rename the directories as well, but you are not doing that. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] conditional renaming folder and files in the tree
banda gunda wrote: > Dear Tutor, > > > I have made some progress! > > But not yet got the results. > > Attached is revised code. > > > Specifically, the problem in below: > > > for root, dirs, files in os.walk(".", topdown=False): > for name in files: > print(os.path.join(root, name)) > os.rename(path + name, path + name.replace("---", "changed")) The `path` variable appears out of the blue here -- it should probably be `root`. Also, you should make it a habit to use os.path.join() to construct filenames, not string concatenation. The reason is that os.path.join() knows when to insert a path separator, and will pick the right one for the platform the script is running on. > #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", > #"changed") Do us (and yourself!) a favour, and remove abandoned code like the line above. It only makes the code harder to follow, and the one who suffers most from that are you. If you are afraid that you will lose valuable snippets put them elsewhere or consider adopting a version control system which allows you to go back to a former version when you find that a change was unsuccessful. > files[name] = os.sep.join([dirpath, name]) That doesn't make sense. What are you trying to achieve here? > > print (files) > > for name in dirs: > print(os.path.join(root, name)) > > > .\---DAT1\---DAT3\---10.txt Note that os.rename() can make a mess of your data. I recommend that you use print statements instead until you are sure that your script does what you want, something like def dryrun_rename(old, new): print("rename", old) print("to", new) print() for parent_dir, dirs, files in os.walk(".", topdown=False): for name in files: old_path = ... # construct filepath from parent_dir and name new_path = ... # construct filepath from parent_dir and modified # name # use os.path.join in both cases dryrun_rename(old_path, new_path) Once this prints what you expect you can swap dryrun_rename for os.rename. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] conditional renaming folder and files in the tree
Dear Tutor, I have made some progress! But not yet got the results. Attached is revised code. Specifically, the problem in below: for root, dirs, files in os.walk(".", topdown=False): for name in files: print(os.path.join(root, name)) os.rename(path + name, path + name.replace("---", "changed")) #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", "changed") files[name] = os.sep.join([dirpath, name]) print (files) for name in dirs: print(os.path.join(root, name)) .\---DAT1\---DAT3\---10.txt --- FileNotFoundError Traceback (most recent call last) in () 2 for name in files: 3 print(os.path.join(root, name)) > 4 os.rename(path + name, path + name.replace("---", "changed")) 5 #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", "changed") 6 files[name] = os.sep.join([dirpath, name]) FileNotFoundError: [WinError 2] The system cannot find the file specified: 'E://---10.txt' -> 'E://changed10.txt' Thanks once again for your time spent on this. best, banda. + From: banda gunda Sent: Friday, August 11, 2017 8:10 AM To: tutor@python.org Subject: conditional renaming folder and files in the tree Dear Tutor, I want to change the name of the folders and the files in the tree. All those folders and files starting with name string '---'. Examples: If a folder name is : \---DAT1 I want to change this to: \changedDAT1 If a file name is: \---1.txt I want to change this to: \changed1.txt I have attached the code and output to this email. Specifically, I like to provide correct syntax (dst / destination) for line 6 code block below! I have not understood the syntax. Thanks in advance, for your help . best, banda + for root, dirs, files in os.walk(".", topdown=False): for name in files: print(os.path.join(root, name)) os.rename(name.replace("---", "changed")) list_of_files[name] = os.sep.join([dirpath, name]) print (list_of_files) for name in dirs: print(os.path.join(root, name)) .\---DAT1\---1.txt --- TypeError Traceback (most recent call last) in () 2 for name in files: 3 print(os.path.join(root, name)) > 4 os.rename(name.replace("---", "changed")) 5 list_of_files[name] = os.sep.join([dirpath, name]) 6 print (list_of_files) TypeError: Required argument 'dst' (pos 2) not found _end_of_email In [1]: import os import sys import glob In [2]: print ("Current directory is %s: " %os.getcwd()) Current directory is C:\Users\mysku\AnacondaProjects: In [3]: path = "E://" # Check current working directory. retval = os.getcwd() print ("Current working directory %s" % retval) # Now change the directory os.chdir( path ) # Check current working directory. retval = os.getcwd() print ("Directory changed successfully %s" % retval) Current working directory C:\Users\mysku\AnacondaProjects Directory changed successfully E:\ In [4]: # listing directories print ("The dir is: %s"%os.listdir(os.getcwd())) The dir is: ['---DAT1', '---DAT2', '---7.txt', '---8.txt', '---9.txt'] In [5]: for root, dirs, files in os.walk(".", topdown=False): for name in dirs: print ("names of folders are: ") print(os.path.join(root, name)) for name in files: print("names of files in folders are: ") print(os.path.join(root, name)) names of files in folders are: .\---DAT1\---DAT3\---10.txt names of folders are: .\---DAT1\---DAT3 names of files in folders are: .\---DAT1\---1.txt names of files in folders are: .\---DAT1\---2.txt names of files in folders are: .\---DAT1\---3.txt names of files in folders are: .\---DAT2\---5.txt names of files in folders are: .\---DAT2\---6.txt names of files in folders are: .\---DAT2\---7.txt names of folders are: .\---DAT1 names of folders are: .\---DAT2 names of files in folders are: .\---7.txt names of files in folders are: .\---8.txt names of files in folders are: .\---9.txt this works for root folders for fileName in os.listdir("."): os.rename(fileName, fileName.replace("---", "changed")) In [6]: for root, dirs,
Re: [Tutor] conditional renaming folder and files in the tree
On 11/08/17 16:10, banda gunda wrote: > for root, dirs, files in os.walk(".", topdown=False): > for name in files: > print(os.path.join(root, name)) > os.rename(name.replace("---", "changed")) Here you give the new name but not the original name. The function needs two values, the source(original name) and the destination(dst) > list_of_files[name] = os.sep.join([dirpath, name]) What is dirpath? This is its first mention. > --- > TypeError Traceback (most recent call last) > in () > > 4 os.rename(name.replace("---", "changed")) > > TypeError: Required argument 'dst' (pos 2) not found See above -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor