Reputation: 2500
I'm struggling for the couple hours to find out how I can write the files into a directory created in the
for
loop.
Mainly I do scripting for data manipulation but want to move forward to work with the os
and subprocesses
and I'm struggling to do this basic stuff when comes to automation at the OS level ...
Problem statement: Create a folder in the for loop in a specified path and for each folder created add some work inside, like: files or export pictures or logs.
Issue I have: folders gets created but the files are getting written to the script path instead the new created folder.
import os
import random
import pathlib
path = "/home/sample/folder/"
for i in range(5):
os.makedirs(os.path.join(path, 'train_set' + str(i)))
print(fd)
wd=os.getcwd()
print(wd)
#tried to use this way but no success ...
#os.chdir(wd)
#os.system ("mkdir "+i)
#dest_fold = dest_path + dname
lst = list(df.index.values)
random.shuffle(lst)
#with open(filename, "w") as f:
for lst in range(1, len(df) , 30):
slc_all = df.iloc[lst : lst + 30]
print(len(slc_all))
count = 0
for j in range(count, len(slc_all)):
...
export_png(p, filename= "file"+str(j)+".png")
Upvotes: 1
Views: 821
Reputation: 82765
This should help.
import os
import random
import pathlib
path = "/home/sample/folder/"
for i in range(5):
dir = os.path.join(path, 'train_set' + str(i))
os.makedirs(dir)
lst = list(df.index.values)
random.shuffle(lst)
#with open(filename, "w") as f:
for lst in range(1, len(df) , 30):
slc_all = df.iloc[lst : lst + 30]
print(len(slc_all))
count = 0
for j in range(count, len(slc_all)):
...
export_png(p, filename= os.path.join(dir, "file"+str(j)+".png")) #Use Full path to save file.
Upvotes: 2