modifying key's name in python dictionary -
i trying write python script can find sub_directories called 'something' within parent directory.then rename sub directories , move them somewhere else. far have code as:
import os, shutil,fnmatch match = {} root, dirnames, filenames in os.walk('test'): #print root #print dirnames #print filenames in fnmatch.filter(dirnames,'find'): #print os.path.join(root,dirnames[0]) print root #match.append(root) match[root]=dirnames[0] call match gives me {'test\a': 'find'......}. modify key value of dictionary looks {'a':'find'... trying rid of name of parent directory. thought converting string , use split seems not efficient.
to dirname without parent directory name, use os.path.basename, way:
>>> dirname = 'c:\\users\\myusers\\videos' >>> os.path.basename(dirname) 'videos' edit: in response op comments,
given:
dirname = 'c:\\users\\myusers\\videos'
and want new folder name myusers_videos, here 1 way:
>>> os.path.basename('_'.join(os.path.split(dirname))) 'myusers_videos'
Comments
Post a Comment