Dor Birendorf
Dor Birendorf

Reputation: 61

python open all files ending with .txt in a relative folder

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

Answers (3)

mama
mama

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

Kushan Gunasekera
Kushan Gunasekera

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:-

  1. os.path.dirname(path)
  2. os.path.join(path, *paths)
  3. glob.glob(pathname, *, recursive=False)

References:-

  1. Open file in a relative location in Python
  2. How to open every file in a folder

Upvotes: 1

xio
xio

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

Related Questions