Reputation: 61
I need to open and parse all files in a folder, but I have to use a relative path (something like ../../input_files/).
I know that in JavaScript you can use the "path" library to solve this problem.
How can I do it in python?
Upvotes: 2
Views: 1639
Reputation: 2227
You can use listdir
from the os
library, and filter out only the files which has txt as the ending
from os import listdir
txts = [x for x in listdir() if x[-3:] == 'txt']
Then you can iterate over the lists and do your work on each file.
Upvotes: 1
Reputation: 8566
Don't worry about the Absolute path, below line gives you the Absolute path where your script runs.
import os
script_dir = os.path.dirname(__file__) # <-- absolute dir to the script is in
Now you can merge your relative path to the Absolute path
rel_path = 'relative_path_to_the_txt_dir'
os.path.join(script_dir, rel_path) # <-- absolute dir to the txt is in
If you print the above line you'll see the exact path your txt
files located on.
Here is what you are looking for:-
import glob
import os
script_dir = os.path.dirname(__file__) # <-- absolute dir to the script is in
rel_path = 'relative_path_to_the_txt_dir'
txt_dir = os.path.join(script_dir, rel_path) # <-- absolute dir to the txt is in
for filename in glob.glob(os.path.join(txt_dir, '*.txt')): # filter txt files only
with open(os.path.join(os.getcwd(), filename), 'r') as file: # open in read-only mode
# do your stuff
Here are few links that you can understand what I did:-
References:-
Upvotes: 1
Reputation: 640
This way you can get a list of files in a path as a list
You can also filter for file types
import glob
for file in glob.iglob('../../input_files/**.**',recursive=True):
print(file)
Here you can specify the file type : **.**
for example: **.txt
Output:
../../input_files/name.type
Upvotes: 3