os.walk

os.walk error of unhandled stopIteration

风流意气都作罢 提交于 2019-12-19 04:40:31
问题 I have written a python script and wanted to debug it using eric ide. When I was running it, an error popped up saying unhandled StopIteration My code snippet: datasetname='../subdataset' dirs=sorted(next(os.walk(datasetname))[1]) I am new to python and so, I don't really know how to fix this. Why is this error popping up and how do I fix it? 回答1: os.walk will generate file names in a directory tree walking it down. It will return the contents for every directory. Since it is a generator it

os.walk error of unhandled stopIteration

二次信任 提交于 2019-12-19 04:40:26
问题 I have written a python script and wanted to debug it using eric ide. When I was running it, an error popped up saying unhandled StopIteration My code snippet: datasetname='../subdataset' dirs=sorted(next(os.walk(datasetname))[1]) I am new to python and so, I don't really know how to fix this. Why is this error popping up and how do I fix it? 回答1: os.walk will generate file names in a directory tree walking it down. It will return the contents for every directory. Since it is a generator it

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

喜你入骨 提交于 2019-12-19 04:04:34
问题 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

os.walk without digging into directories below

安稳与你 提交于 2019-12-17 15:06:13
问题 How do I limit os.walk to only return files in the directory I provide it? def _dir_list(self, dir_name, whitelist): outputList = [] for root, dirs, files in os.walk(dir_name): for f in files: if os.path.splitext(f)[1] in whitelist: outputList.append(os.path.join(root, f)) else: self._email_to_("ignore") return outputList 回答1: Use the walklevel function. import os def walklevel(some_dir, level=1): some_dir = some_dir.rstrip(os.path.sep) assert os.path.isdir(some_dir) num_sep = some_dir.count

os.walk without hidden folders

天大地大妈咪最大 提交于 2019-12-17 07:29:03
问题 I need to list all files with the containing directory path inside a folder. I tried to use os.walk , which obviously would be the perfect solution. However, it also lists hidden folders and files. I'd like my application not to list any hidden folders or files. Is there any flag you can use to make it not yield any hidden files? Cross-platform is not really important to me, it's ok if it only works for linux (.* pattern) 回答1: No, there is no option to os.walk() that'll skip those. You'll

Travel directory tree with limited recursion depth

﹥>﹥吖頭↗ 提交于 2019-12-17 06:49:09
问题 I need to process all files in a directory tree recursively, but with a limited depth. That means for example to look for files in the current directory and the first two subdirectory levels, but not any further. In that case, I must process e.g. ./subdir1/subdir2/file , but not ./subdir1/subdir2/subdir3/file . How would I do this best in Python 3? Currently I use os.walk to process all files up to infinite depth in a loop like this: for root, dirnames, filenames in os.walk(args.directory):

How do I exclude directories when using os.walk()? Other methods haven't worked

柔情痞子 提交于 2019-12-14 02:23:40
问题 So far the following code has been nothing but stubborn: for root,subdirs,files in os.walk(topDir): for fileName in files: if fileName.endswith(("0.tif","0.png")): images.update({fileName[:-5]:Image(fileName,origin)}) elif fileName.endswith((".tif",".png")): try: images.update({fileName[:-4]:Image(fileName,picLocations[fileName[:-4]])}) except: images.update({fileName[:-4]:Image(fileName,origin)}) else: pass I've tried making the first three lines read: exclude = set(["faces","animations"])

Using os.walk in Python

China☆狼群 提交于 2019-12-13 02:54:07
问题 I am trying to replace a character in multiple files in multiple subdirectories (over 700 files in 50 or so subfolders). This files works if I remove the path and place the file in the specific folder; however, when I try and use the os.walk function to go through all subdirectories, I get the following error: [Error 2] The system cannot find the file specified It points to the last line of my code. Here is the code in its entirety: import os path = "C:\Drawings" for root, dirs, files in os

Os.walk wont work with directory acquired through ' match.group(0).encode('string-escape') '

[亡魂溺海] 提交于 2019-12-11 19:33:01
问题 I'm using os.walk to search for files in specific directories. This is testcode that wont do what it should: import os, re cwd = os.getcwd() directory= 'Box II' dirpattern = re.compile(r'^.*?\\'+directory+'.*?', re.M) for root, dirs, files in os.walk(os.path.abspath(cwd)): if dirpattern.search(root): match = dirpattern.search(root) match = match.group(0).encode('string-escape') print match '''OUTPUT = D:\\dir1\\dir2\\dir3''' for roots, dirss, filess in os.walk(match): print filess '''OUPUT =

Python Batch convert of FLAC files

纵饮孤独 提交于 2019-12-11 18:16:01
问题 I want to convert all FLAC files to OGG in the working directory: This is what I ALREADY have. for root, dirs, files in os.walk(args): flacs = [f for f in files if f.endswith('.flac')] oggs = [o for o in files if o.endswith('.ogg')] for flacfiles in flacs: id3 = ('id3v2', '-C', flacfiles) cmd = ('oggenc', '-q7', flacfiles) try: subprocess.check_call(id3, cwd=root) subprocess.check_call(cmd, cwd=root) except subprocess.CalledProcessError: print "subprocess.CalledProcessError: Command %s