Reputation: 182
I want to find a file and open it! Right now I have some problems! Basically, I don't know how to find the file, I know how to find a file in the same directory but not globally on the computer! Can anyone help me? Hier is my code
import os
for root, dirs, files in os.walk(".txt"):
for filename in files:
os.startfile(filename)
Upvotes: 1
Views: 1995
Reputation: 20568
Reading the fine documentation would be a good place to start.
"Globally on the computer" means /
slash.
Start there, or perhaps in your home directory.
import os
for root, dirs, files in os.walk('/'):
for file in files:
if file.endswith('.txt'):
filename = os.path.join(root, file)
os.startfile(filename)
Upvotes: 1
Reputation: 375
You can try my answer at:
https://stackoverflow.com/questions/2212643/python-recursive-folder-read/55193831#55193831
code:
import glob
import os
root_dir = <root_dir_here>
for filename in glob.iglob(root_dir + '**/**', recursive=True):
if os.path.isfile(filename):
with open(filename,'r') as file:
print(file.read())
Upvotes: 0