os.walk

Python directory searching and organizing by dict

余生长醉 提交于 2019-12-11 14:48:16
问题 Hey all, this is my first time recently trying to get into the file and os part of Python. I am trying to search a directory then find all sub directories. If the directory has no folders, add all the files to a list. And organize them all by dict. So for instance a tree could look like this Starting Path Dir 1 Subdir 1 Subdir 2 Subdir 3 subsubdir file.jpg folder1 file1.jpg file2.jpg folder2 file3.jpg file4.jpg Even if subsubdir has a file in it, it should be skipped because it has folders in

Recursion definition using Python os.walk as an example

梦想的初衷 提交于 2019-12-11 09:04:11
问题 I am having trouble grasping how to recursively list files from a directory using python. Does all the recursion logic take place in the module itself (os.walk)? def listfiles(path): for root, dirs, files in os.walk(path): for f in files: print(f) I have looked up multiple ways to recursively list files in a directory and they all follow the same pattern as above. This appears to be iterating through the files. Could someone explain to me how this is being recursively done? 回答1: os.walk() is

Readline feature in Directory Lister class

最后都变了- 提交于 2019-12-11 02:47:19
问题 The below class is a dynamic attribute generating directory walker by Anurag. import os class DirLister(object): def __init__(self, root): self.root = root self._list = None def __getattr__(self, name): try: var = super(DirLister).__getattr__(self, name) return var except AttributeError: return DirLister(os.path.join(self.root, name)) def __str__(self): self._load() return str(self._list) def _load(self): """ load once when needed """ if self._list is not None: return self._list = os.listdir

How to handle OSX Aliases in Python with os.walk()?

帅比萌擦擦* 提交于 2019-12-10 20:35:24
问题 I'm traversing a directory tree using Python 2.7.x, getting the file and directory sizes as it traverses. The problem I'm running into is that it is mistaking alias files for directories, and then throwing an error that there's "No Such File or Directory". Code below: 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_size for dirName,

os.walk with regex

依然范特西╮ 提交于 2019-12-06 09:38:48
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. 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_, dirnames, filenames in os.walk(dirpath): for filename in filenames: abspath = os.path.join(dir_, filename) if

python storing path names with forward vs backward slash

China☆狼群 提交于 2019-12-06 04:32:39
I have a procedure that os.walk s a directory and its subdirectories to filter pdf files, separating out their names and corresponding pathnames. The issue I am having is that it will scan the topmost directory and print the appropriate filename e.g. G:/Books/Title.Pdf but the second it scans a subfolder e.g G:/Books/Sub Folder/Title.pdf it will print the following G:/Books/Sub Folder\\Title.Pdf (which is obviously an invalid path name). It will also add \\ to any subfolders within subfolders. Below is the procedure: def dicitonary_list(): indexlist=[] #holds all files in the given directory

How to rename files using os.walk()?

一笑奈何 提交于 2019-12-04 06:24:11
问题 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.walk multiple directories at once [duplicate]

大兔子大兔子 提交于 2019-12-03 16:58:31
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to join two generators in Python? Is there a way in python to use os.walk to traverse multiple directories at once? my_paths = [] path1 = '/path/to/directory/one/' path2 = '/path/to/directory/two/' for path, dirs, files in os.walk(path1, path2): my_paths.append(dirs) The above example doesn't work (as os.walk only accepts one directory), but I was hoping for a more elegant solution rather than calling os

using os.walk cannot open the file from the list

我的未来我决定 提交于 2019-12-02 17:53:09
问题 My problem is to read '.csv' files in catalogs and do some calculations on them. I have calculations working but my for loop seem not to work as I want to. d = 'F:\MArcin\Experiments\csvCollection\' for dirname, dirs, files in os.walk(d): for i in files: if i.endswith('.csv'): data1 = pd.read_csv(i, sep=",") data = data1['x'][:, np.newaxis] target = data1['y'] The error Iam getting is: IOError: File 1.csv does not exist files is list of all '.csv' files inside dirname i is str of size 1 and

Quicker to os.walk or glob?

这一生的挚爱 提交于 2019-12-02 17:40:56
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, files in os.walk("/path/to/dir/") for file in files: if core.search(file): path = os.path.join(root