0

I have two paths each path contains many files, each file contains data for one day. I need to read the file from the first path, and the other file from the other path that corresponds to the same day. [in the same python loop I want to read the file of the first day in each path]. The files are with the same name and sequence in each path. I'm trying to use os.listdir(path) in for loop instead of with open (file) as file because I want to read the file as a data frame using pandas. Then use pandas to do data aggregation for each file.

8
  • How do you know which file in the second path corresponds to each file in the first path, without openning them? Commented Oct 10, 2019 at 8:12
  • Arethe number of files and their sequence same? More importantly what is the naming convention for the files? Commented Oct 10, 2019 at 8:13
  • The files have the same names and order regarding the date as (20191010.csv in the first path and 20191010 in the second path). in other words the first file in the first path with the first path in the second path and so on so forth@Aryerez Commented Oct 10, 2019 at 8:15
  • The sequence of files and order in two paths are the same @Sayandip Dutta Commented Oct 10, 2019 at 8:16
  • with open("file1_path") as a, open("file2_path") as b: .... All you need is to construct the path for both files. Commented Oct 10, 2019 at 8:17

1 Answer 1

1

I assume, you are sure, that a file always exists in both directories.

Is this what you are asking?

path1 = "path1"
path2 = "path2"
for fname in os.listdir(path1):
    fname1 = os.path.join(path1, fname)
    fname2 = os.path.join(path2, fname)
        # do your processing here

If it can happen, that a file exists in path1 but not in path 2 you have to check with os.path.isfile() the presence of the other file before opening and skip if absent

Sign up to request clarification or add additional context in comments.

1 Comment

changed my answer to not use open

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.