os.walk

Rename files to instead being in sub-directories, have the year as part of the filename

走远了吗. 提交于 2019-12-02 17:28:44
问题 Create a copy of the CarItems tree called CarItemsCopy where all files, instead of being in directories named after years, rather have the year as part of the filename, and the year directories are entirely absent. Instead of some of these examples: CarItems/Chevrolet/Chevelle/2011/parts.txt CarItems/Chevrolet/Chevelle/1982/parts.txt CarItems/Chevrolet/Volt/1994/parts.txt it should look like this: CarItemsCopy/Chevrolet/Chevelle/parts-2011.txt CarItems/Chevrolet/Chevelle/parts-1982.txt

Return list of the paths of all the parts.txt files

爱⌒轻易说出口 提交于 2019-12-02 14:26:36
问题 Write a function list_files_walk that returns a list of the paths of all the parts.txt files, using the os module's walk generator. The function takes no input parameters. def list_files_walk(): for dirpath, dirnames, filenames in os.walk("CarItems"): if 'parts.txt' in dirpath: list_files.append(filenames) print(list_files) return list_files The output (list_files) is supposed to look similar to this: CarItems/Chevrolet/Chevelle/2011/parts.txt CarItems/Chevrolet/Chevelle/1982/parts.txt How

How to rename files using os.walk()?

*爱你&永不变心* 提交于 2019-12-02 07:57:50
I'm trying to rename a number of files stored within subdirectories by removing the last four characters in their basename. I normally use glob.glob() to locate and rename files in one directory using: import glob, os for file in glob.glob("C:/Users/username/Desktop/Original data/" + "*.*"): pieces = list(os.path.splitext(file)) pieces[0] = pieces[0][:-4] newFile = "".join(pieces) os.rename(file,newFile) But now I want to repeat the above in all subdirectories. I tried using os.walk() : import os for subdir, dirs, files in os.walk("C:/Users/username/Desktop/Original data/"): for file in files:

How to improve searching with os.walk and fnmatch

主宰稳场 提交于 2019-12-01 21:43:53
问题 I'm using os.walk and fnmatch with filters to search a pc's hdd for all image files. This works perfectly fine but is extremely slow since it takes about 9 minutes to search +-70000 images. Any ideas on optimizing this code to run faster? Any other suggestions? I'm using python 2.7.2 by the way. import fnmatch import os images = ['*.jpg', '*.jpeg', '*.png', '*.tif', '*.tiff'] matches = [] for root, dirnames, filenames in os.walk("C:\\"): for extension in images: for filename in fnmatch.filter

How to improve searching with os.walk and fnmatch

一曲冷凌霜 提交于 2019-12-01 20:09:20
I'm using os.walk and fnmatch with filters to search a pc's hdd for all image files. This works perfectly fine but is extremely slow since it takes about 9 minutes to search +-70000 images. Any ideas on optimizing this code to run faster? Any other suggestions? I'm using python 2.7.2 by the way. import fnmatch import os images = ['*.jpg', '*.jpeg', '*.png', '*.tif', '*.tiff'] matches = [] for root, dirnames, filenames in os.walk("C:\\"): for extension in images: for filename in fnmatch.filter(filenames, extension): matches.append(os.path.join(root, filename)) I'm not one of those regex maniacs

Efficiently removing subdirectories in dirnames from os.walk

帅比萌擦擦* 提交于 2019-12-01 05:45:00
On a mac in python 2.7 when walking through directories using os.walk my script goes through 'apps' i.e. appname.app, since those are really just directories of themselves. Well later on in processing I am hitting errors when going through them. I don't want to go through them anyways so for my purposes it would be best just to ignore those types of 'directories'. So this is my current solution: for root, subdirs, files in os.walk(directory, True): for subdir in subdirs: if '.' in subdir: subdirs.remove(subdir) #do more stuff As you can see, the second for loop will run for every iteration of

Efficiently removing subdirectories in dirnames from os.walk

泄露秘密 提交于 2019-12-01 03:20:11
问题 On a mac in python 2.7 when walking through directories using os.walk my script goes through 'apps' i.e. appname.app, since those are really just directories of themselves. Well later on in processing I am hitting errors when going through them. I don't want to go through them anyways so for my purposes it would be best just to ignore those types of 'directories'. So this is my current solution: for root, subdirs, files in os.walk(directory, True): for subdir in subdirs: if '.' in subdir:

Copying specific files to a new folder, while maintaining the original subdirectory tree

泪湿孤枕 提交于 2019-11-30 23:13:46
I have a large directory with many subdirectories that I am trying to sort, I am trying to copy specific file types to a new folder, but I want to maintain the original subdirectories. def copyFile(src, dest): try: shutil.copy(src,dest) except shutil.Error as e: print('Error: %s' % e) except IOError as e: print('Error: %s' % s.strerror) for root, directories, files in os.walk(directory): for directoryname in directories: dirpath = os.path.join(root,directoryname) dir_paths.append(dirpath) dir_names.append(directoryname) if not os.listdir(dirpath): #Cheching if directory is empty print("Empty")

os.walk() ValueError: need more than 1 value to unpack

旧时模样 提交于 2019-11-30 20:35:57
Alright, I'm working with a Bioloid Premium humanoid robot, and Mac OS X will not recognize it. So I wrote a Python script to detect changes in my /dev/ folder because any connection on a Linux-based system is still given a reference via a file descriptor. My code should work, however, when assigning three variable to the values that are returned by os.walk(top), I get a ValueError. Anyone know how I can fix this? I've used this function in the past, and it hasn't given me any trouble. My script btw is very rough, I wrote it in about 5 minutes or so. Code: root_o, dir_o, files_o = os.walk(top)

Need the path for particular files using os.walk()

微笑、不失礼 提交于 2019-11-30 11:54:30
问题 I'm trying to perform some geoprocessing. My task is to locate all shapefiles within a directory, and then find the full path name for that shapefile within the directory. I can get the name of the shapefile, but I don't know how to get the full path name for that shapefile. shpfiles = [] for path, subdirs, files in os.walk(path): for x in files: if x.endswith(".shp") == True: shpfiles.append[x] 回答1: os.walk gives you the path to the directory as the first value in the loop, just use os.path