I am executing an imported method in my Python program which generates a bunch of files in the current directory. When I execute it I want to stay in the current directory and instead want it to generate the files in a different directory that I make on the fly, without changing to this directory using os.chdir
. What I currently have is that I make the dir, then I change to it using os.chdir
and finally change back to the base dir.
Let's say cwd is /home/user/project/
,
I create /home/user/project/uploads/files/
the files directory is created in the uploads dir and the files are subsequently generated here here. Lastly I switch back to the base dir /home/user/project/
base_dir = os.getcwd()
os.mkdir("/home/user/project/uploads/files/")
os.chdir("/home/user/project/uploads/files/")
method()
os.chdir(base_dir)
Is there a more efficient way to do this? Thanks in advance.