os.walk

How to stop traversing current root and iterate to the next one

纵饮孤独 提交于 2020-01-05 06:41:39
问题 I'm writing a simple function that walks through a directory tree looking for folders of a certain name. What I'm after is a match's parent path. E.g., for "C:/a/b/c/MATCH" I want "C:/a/b/c". I don't need duplicate parents or paths to subfolder matches, so if there's a "C:/a/b/c/d/e/f/MATCH", I don't need it. So, during my walk, once I have a parent, I want to iterate to the next current root. Below is what I have so far, including a comment where I'm stuck. def FindProjectSubfolders

python Mac OS : os.path.getsize returns different value than du -ks?

强颜欢笑 提交于 2020-01-04 09:42:09
问题 When comparing the size of a directory with Unix and python, I have slightly different results (5% smaller with "disk usage"). Why ? (all my subfolders are readable; I work under Mac OSX Mountain lion, python version 2.7.2) Here is my code : import os, sys from commands import getstatusoutput def get_size(start_path = '.'): total_size = 0 for dirpath, dirnames, filenames in os.walk(start_path): for f in filenames: fp = os.path.join(dirpath, f) total_size += os.path.getsize(fp) return total

os.walk with regex

江枫思渺然 提交于 2020-01-02 20:58:09
问题 I'd like to get a list of files that apply to a regex that i have. I guess i should use os.walk, but how can i use it with regex? Thanks. 回答1: I'm not aware of anything in the stdlib implementing this, but it is not hard to code: import os, os.path def iter_matching(dirpath, regexp): """Generator yielding all files under `dirpath` whose absolute path matches the regular expression `regexp`. Usage: >>> for filename in iter_matching('/', r'/home.*\.bak'): .... # do something """ for dir_,

Extending Python's os.walk function on FTP server

丶灬走出姿态 提交于 2020-01-01 09:01:50
问题 How can I make os.walk traverse the directory tree of an FTP database (located on a remote server)? The way the code is structured now is (comments provided): import fnmatch, os, ftplib def find(pattern, startdir=os.curdir): #find function taking variables for both desired file and the starting directory for (thisDir, subsHere, filesHere) in os.walk(startdir): #each of the variables change as the directory tree is walked for name in subsHere + filesHere: #going through all of the files and

Quicker to os.walk or glob?

夙愿已清 提交于 2019-12-31 08:28:53
问题 I'm messing around with file lookups in python on a large hard disk. I've been looking at os.walk and glob. I usually use os.walk as I find it much neater and seems to be quicker (for usual size directories). Has anyone got any experience with them both and could say which is more efficient? As I say, glob seems to be slower, but you can use wildcards etc, were as with walk, you have to filter results. Here is an example of looking up core dumps. core = re.compile(r"core\.\d*") for root, dirs

os.walk very slow, any way to optimise?

霸气de小男生 提交于 2019-12-29 01:39:12
问题 I am using os.walk to build a map of a data-store (this map is used later in the tool I am building) This is the code I currently use: def find_children(tickstore): children = [] dir_list = os.walk(tickstore) for i in dir_list: children.append(i[0]) return children I have done some analysis on it: dir_list = os.walk(tickstore) runs instantly, if I do nothing with dir_list then this function completes instantly. It is iterating over dir_list that takes a long time, even if I don't append

Filtering os.walk() dirs and files

左心房为你撑大大i 提交于 2019-12-27 17:00:23
问题 I'm looking for a way to include/exclude files patterns and exclude directories from a os.walk() call. Here's what I'm doing by now: import fnmatch import os includes = ['*.doc', '*.odt'] excludes = ['/home/paulo-freitas/Documents'] def _filter(paths): matches = [] for path in paths: append = None for include in includes: if os.path.isdir(path): append = True break if fnmatch.fnmatch(path, include): append = True break for exclude in excludes: if os.path.isdir(path) and path == exclude:

Python finds a string in multiple files recursively and returns the file path

两盒软妹~` 提交于 2019-12-25 07:27:37
问题 I'm learning Python and would like to search for a keyword in multiple files recursively. I have an example function which should find the *.doc extension in a directory. Then, the function should open each file with that file extension and read it. If a keyword is found while reading the file, the function should identify the file path and print it. Else, if the keyword is not found, python should continue. To do that, I have defined a function which takes two arguments: def find_word

os.walk() not processing subdirectories when using UNC paths

五迷三道 提交于 2019-12-25 07:26:44
问题 I'm having trouble with os.walk() in python 2.7.8 on Windows. When I supply it with a 'normal' path such as "D:\Test\master" it works as expected. However when I supply it with a UNC path such as "\\?\D:\Test\master" it will report the root directory as expected but it will not drill down into the sub directories, nor will it raise an exception. My research: I read on the help page that os.walk() accepts a function argument to handle errors. By default this argument is None so no error is

How do I pass Biopython SeqIO.convert() over multiple files in a directory?

。_饼干妹妹 提交于 2019-12-23 05:20:38
问题 I’m writing a python script (version 2.7) that will change every input file (.nexus format) within the specified directory into .fasta format. The Biopython module SeqIO.convert handles the conversion perfectly for individually specified files but when I try to automate the process over a directory using os.walk I’m unable to correctly pass the pathname of each input file to SeqIO.convert. Where are I going wrong? Do I need to use join() from os.path module and pass the full path names on to