Reputation: 2557
In my code, I create a textfile for the stdout and also save several .png images and .mat matrices - when the code finishes running there are a lot of files inside the directory
I want the code to be able to create a new directory inside the folder where my code is running, and save the .txt file as well as the output .png and .mat to this newly created folder.
I have figured out that to create the new directory I should do:
import os
os.mkdir('folder')
And to create the output file and set the stdout there it is
import sys
filename = open('filename.txt','w')
sys.stdout = filename
I tried using open('folder/filename.txt','w') but i get the error: IOError: [Errno 2] No such file or directory
Thank you!
Upvotes: 0
Views: 3751
Reputation: 78
If I understand you right, you want to create the file 'filename.txt' inside the folder you just made ('folder')?
Given that's the case, use os.path.join()
import sys
filename = open(os.path.join('folder','filename.txt'),'w')
sys.stdout = filename
Now sys.stdout points to the file which is inside the new folder
Upvotes: 1