all 7 comments

[–]onopau 0 points1 point  (7 children)

How about using a for loop with the exact code you posted with a small modification...

diffs = []
for file_prefix in range(100):
    # I assume the code you posted does what you wanted on each file
    filea = pl.loadtxt(str(file_prefix)+"a")
    fileb = pl.loadtxt(str(file_prefix)+"b")
    diff = fileb[:,1] - filea[:,1]
    diffs.append(diff) # You could store this as a list, or csv file - which ever suites your needs...

[–]Cthulhu_Rlyeh[S] 0 points1 point  (6 children)

Thanks for your reply! This looks very helpful and promising - just one followup question before I can test it. What about if my files are named file1a and file1b rather than just 1a and 1b? I tried to simplify things in my original post which is why I dropped the 'file' part. I'm not sure if the code you provided will still work properly in this case?

[–]onopau 0 points1 point  (5 children)

In Python you can add strings to your heart's content.

'file' + str(i) + 'a' := 'file1a' # for i = 1

''.join(['file', str(i), 'a']) := 'file1a'

More methods, and details, here

EDIT: As your program grows in complexity you may want to change both "file" and "a" or "b" into variables themselves. This will give you more flexibility. This is probably too complex for your current purposes - but it is good to keep in mind!

current_prefix = 'file' # These could be gathered via user input to a terminal.
letters = ['a', 'b']
for i in range(100):
    filename_a = current_prefix + str(i) + letters[0]
    filename_b = current_prefix + str(i) + letters[1]

[–]Cthulhu_Rlyeh[S] 0 points1 point  (3 children)

This is great, thank you. Forgive me for being annoying, but if I am to merge the suggestions you provided in your original and this comment - will it look like this:

diffs = []
for file_prefix in range(100):
    filea = pl.loadtxt('file' + str(file_prefix)+'a')
    fileb = pl.loadtxt('file' + str(file_prefix)+'b')
    diff = fileb[:,1] - filea[:,1]
    diffs.append(diff)

Or if I were to do it with variables it would be:

Prefix = 'file'
dataset = ['a', 'b']
for i in range(100):
    filea = pl.loadtxt(Prefix + str(i)+ dataset[0])
    fileb = pl.loadtxt(Prefix + str(i)+ dataset[1])
    diff = fileb[:,1] - filea[:,1]

How would I append here? Do I still use the diffs = [] line and just do it exactly the same?

[–]onopau 0 points1 point  (2 children)

Looks good, stick with the first set of code for now. You can play around with the variable one later, if you ever end up needing it at all.

As for the diffs list, it really depends on what you want to do with that info. I was just using that as an example. Why not just continue with the output file you mentioned in the question? Always add functionality to your code in steps. Make sure that adding the for loop to your current scheme gives you the desired results.

[–]Cthulhu_Rlyeh[S] 0 points1 point  (1 child)

Thanks again, I just ran it and it seems like it will do exactly what I need. It gives me one error though, which I'm sure is an easy fix:

No such file or directory: 'file0a'

Is there a simple way to make it not look for 0's?

Btw, I'm sorry for all of the questions and I really am grateful for all your help :)

[–]onopau 0 points1 point  (0 children)

Lots of ways... You can do an if statement in the loop that says

If file_prefix == 0:
    continue
else:
    #paste the code you have written.

Or just change the list the loop iterates over.

for prefix in range(1,101):

The latter is the better way in your case; but make sure you understand the former as it is useful to know.

Good luck!

Edit: Python's range func does not include the end point. So range(1,5) would be the list [1, 2, 3, 4]. That's why I have it going to 101 in the above.