For os.walk('/one/two/three/'), it'll be '/one/two/three/' the second one might be '/one/two/three/four'. For os.walk('./two/three'), it'll be './two/three'. So, for os.walk('three'), the root in the first tuple will be 'three' (for os.walk('three/'), it'll be 'three/'). One final point the root directory paths always begin with the path that you pass to os.walk(), whether it was relative to your current working directory or not. It is also predicted that according to benchmarks it is faster than other methods to match pathnames in directories. Another way to think of it is that the directories and files in dirs and files will never have slashes in them. The pattern rules of glob follow standard Unix path expansion rules. for root, dirs, files in os. I am trying to recursively list all file names that are in sub directories called Oracle (but not list files in other sub directories). python globasterisk.py dir/file.txt dir/file1.txt dir/file2.txt dir/filea.txt dir/fileb. Note that they are the names, not the paths or to be precise, they're the path of that directory/file relative to the current root directory, which is another way of saying the same thing. Recursive listing of files in a directory matching a pattern. The pattern matches every pathname (file or directory) in the directory dir, without recursing further into subdirectories. Os.walk() returns a generator that gives you tuples like (root, dirs, files), where root is the current directory, and dirs and files are the names of the directories and files, respectively, that are in the root directory. If 'gen_dts' in open(filepath, 'r').read(): What you need to do is open(os.path.join(root, name), 'r') (I added the mode since it's good practice). You're getting that error because you're trying to open name, which is just the file's name, not it's full relative path. import os userinput input ('What is the name of your directory') directory os.listdir (userinput) searchstring input ('What word are you trying to find') for fname in directory: if os.path.isfile (userinput + os.sep + fname): Full path f open (userinput + os.sep + fname, 'r') if searchstring in f.read (): print ('found st.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |