0

I have directory A.
A contains a.txt, b.txt, c.txt.

I have directory B,
B contains a.csv, b.csv, c.csv.

I want am able to read and process files both separately as follows:

a.py

src = '/home/A'
file_paths = [os.path.join(src, f) for f in os.listdir(src)]
for file in file_paths:

    for line in open(file, 'rb').readlines():
    #do some some
    # print some stuff  

b.py

src = '/home/B'
file_paths = [os.path.join(src, f) for f in os.listdir(src)]
for file in file_paths:

    with open(filename, 'r') as ff:
    for line in ff:
        _, end, label = line.strip().split('\t')

    #do some some
    # print some stuff  

So results from a.py look like :

file_processed: a.txt   
output    
file processed: b.txt  
output  
file processed: c.txt  
output

and similarly from b.py

file_processed: a.csv   
output    
file processed: b.csv  
output  
file processed: c.csv  
output

I don't want to do this ^ process as it is hard to visually compare results of a.csv with a.txt because they are on two different terminals or files and so on.

What I want to do is :read files with same name from dir's at once and then perform computations and then print the results

Example: I should read a.txt from /A and process it , and then read a.csv from /B and process it. then print both results. Then move on to b.txt from /A and b.csv from /Band so on.

Please advise. Let me know if you want me to update the logic for do some stuff portion of the code.

2
  • 2
    Please provide a minimal reproducible example. Also, why would use use C-style comments in your Python code? Use python comments. Commented Feb 26, 2018 at 18:59
  • sorry about that, I will change that. Commented Feb 26, 2018 at 19:19

2 Answers 2

1

Just form both lists of filepaths then loop over them at the same time.

a_root = '/home/A'
b_root = '/home/B'
a_paths = [os.path.join(a_root, f) for f in os.listdir(a_root)]
b_paths = [os.path.join(b_root, f) for f in os.listdir(b_root)]
for a_path, b_path in zip(a_paths, b_paths):
    with open(a_path) as a_file, open(b_path) as b_file:
        a_data = a_file.read()
        b_data = b_file.read()
    # do stuff with a and b
Sign up to request clarification or add additional context in comments.

Comments

1

Example: I should read a.txt from /A and process it , and then read a.csv from /B and process it. then print both results. Then move on to b.txt from /A and b.csv from /Band so on.

I feel like you have already answered your own question. This even works as pseudocode. Here is an inelegant but effective example.

filenames = [f for f in however_you_scrape_you_filenames]
directory_extension_map = {'/path/to/a': 'txt',
                           '/path/to/b': 'csv'
                          }
for fname in filenames:
    for directory in directory_extension_map:
        extension = directory_extension_map[directory]
        file_path = os.path.join(directory, f'{fname}.{extension}')
        # Read and parse file_path

I feel like you can take it from here.

1 Comment

How did it go? Did this answer your question?

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.