0

I have a piece of code which runs four other pieces of code. However when those codes run they write their out put writes to the directory I run this code in. I was wondering if there is a way to run the code so it changes directory after every time, here is my code:

loop=np.arange(187761,187854)
for ext in loop:
 ext=str(ext)
 s0='' 
 dcom=["rm *.txt"]
 dcom=s0.join(dcom)
 nes=os.system(dcom)

 a=sys.argv[1]
 com=['cd /cygdrive/e/desar2.cosmology.illinois.edu+7443/DESFiles/desardata/OPS/red/', a,'/red/DECam_00',ext, '/']
 com=s0.join(com)
 print com
 sres=os.system(com)

 com=['python /home/pythoncodes/other4.py ', '00'+ext]
 com=s0.join(com)
 print com
 sres=os.system(com)

 com=['python /home/pythoncodes/correctedmo.py ', '00'+ext]
 com=s0.join(com)
 print com
 sres=os.system(com)

 com=['python /home/pythoncodes/graphs.py ', '00'+ext]
 com=s0.join(com)
 print com
 sres=os.system(com)

 com=['python /home/pythoncodes/whiskerother4.py ', '00'+ext]
 com=s0.join(com)
 print com
 sres=os.system(com)

I was hoping the top part would change the directory for every ext, but it doesn't. Is there a way to do this or should I just alter the programs to write to different directories?

astrochris
  • 1,756
  • 5
  • 20
  • 42

2 Answers2

2

You can use os.chdir(path) to change the current directory.

hivert
  • 10,579
  • 3
  • 31
  • 56
0

I don't know Python so no code in this answer. What I understand is that if you run the script under directory /foo/, the script outputs to /foo/bar.txt. If you change the directory to faa, you'll output to /faa/bar.txt. Next, you want a way of always writing to a specific directory no matter what dir the script was executed in.

You can change the directory your code works in but that's probably not what you want to do. You need to output your file to the target directory in one pass, eg. write("~/static_dir/"+file_name, file_contents).

The reason is very simple:

  • Fewer commands
  • Harder to get lost in directories
rath
  • 3,655
  • 1
  • 40
  • 53