mcrouch
mcrouch

Reputation: 41

writing a python script that calls different directories

It's kind of hard to explain but I'm using a directory that has a number of different files but essentially I want to loop over files with irregular intervals

so in pseudocode I guess it would be written like:

A = 1E4, 1E5, 5E5, 7E5, 1E6, 1.05E6, 1.1E6, 1.2E6, 1.5E6, 2E6

For A in range(start(A),end(A)):
   inputdir ="../../../COMBI_Output/Noise Studies/[A] Macro Particles/10KT_[A]MP_IP1hoN0.0025/"

Run rest of code

Because at the moment I'm doing it manually by changing the value in [A] and its a nightmare and time consuming. I'm using Python on a macbook so I wonder if writing a bash script that is called within Python would be the right idea?

Or replacing A with a text file, such that its:

import numpy as np

mpnum=np.loadtxt("mp.txt")
for A in range(0,len(A)):
   for B in range(0,len(A)):
       inputdir ="../../../COMBI_Output/Noise Studies/",[A] "Macro Particles/10KT_",[A]"MP_IP1hoN0.0025/"

But I tried this first and still had no luck.

Upvotes: 0

Views: 80

Answers (2)

Izy-
Izy-

Reputation: 1163

The idea of putting the file names in a list and simply iterating over them using for a in A: seems to be the best idea. However, one small suggestion, if I may, instead of having a list, if you're going to have a large number of files inside this list, why not make it a dictionary? In this way, you can iterate through your files easily as well as keep a count on them.

Upvotes: 0

Rod
Rod

Reputation: 55752

You are almost there. You don't need a range, just iterate over the list. Then do a replacement in the string using format.

A = ['1E4', '1E5', '5E5', '7E5', '1E6', '1.05E6', '1.1E6', '1.2E6', '1.5E6', '2E6']
for a in A:
   inputdir = "../../../COMBI_Output/Noise Studies/{} Macro Particles/10KT_{}MP_IP1hoN0.0025/".format(a)

Upvotes: 1

Related Questions