R
Railway•17mo ago
ananthankichu

Issue while creating a directory

Hey, I am trying to run a Django application . There is a section where I want to create a directory programmatically. When I am trying to create a directory, it is not giving any error, but it is not creating the directory. could you please help me to find the solution?
20 Replies
Percy
Percy•17mo ago
Project ID: 06a78b22-a3ba-46f2-a16d-2f2ee81b0ed9
ananthankichu
ananthankichuOP•17mo ago
06a78b22-a3ba-46f2-a16d-2f2ee81b0ed9
Brody
Brody•17mo ago
does this work locally
ananthankichu
ananthankichuOP•17mo ago
Yes. It works locally
Brody
Brody•17mo ago
what directory are you creating these subdirectories in
ananthankichu
ananthankichuOP•17mo ago
current_dir = os.getcwd() output_dir = "store/random_string" full_output_dir = os.path.join(current_dir, output_dir) if not os.path.exists(full_output_dir): print('PATH DOESNOT EXISTS- CREATING') os.makedirs(full_output_dir) else: print('PATH EXISTS CONTINUING') There is a directory named "store" in the project. I am trying to create a directory inside the "store" directory. Above is the code snippet
Brody
Brody•17mo ago
I see two print statements there, and neither of them print on railway?
ananthankichu
ananthankichuOP•17mo ago
print('PATH DOESNOT EXISTS- CREATING') This is getting printed. But the directory is not getting created
Brody
Brody•17mo ago
check for errors from os.makedirs
ananthankichu
ananthankichuOP•17mo ago
try: os.makedirs(full_output_dir, exist_ok=False) print('NO ERRORS') except Exception as e: print(e) I tried this. There are no errors.
Brody
Brody•17mo ago
okay and what makes you think the directory wasn't created
ananthankichu
ananthankichuOP•17mo ago
There is another function that uses the newly created directory. When I run that function, I am getting error directory doesn't exist. For confirming it, I made a simple view to check the contents of the directory, Below is the code. def testDirView(request): dir = request.GET.get('dir') directories = [name for name in os.listdir(dir)] print('DIR--->> ',directories) return HttpResponse("DONE") This doesn't print the newly created directory.
Brody
Brody•17mo ago
okay I will do some tests tomorrow as it's very late for me right now
ananthankichu
ananthankichuOP•17mo ago
ok. Thanks. 🙂
Brody
Brody•17mo ago
you keep debugging this too!
ananthankichu
ananthankichuOP•17mo ago
sure.
Brody
Brody•17mo ago
im sorry i couldn't reproduce your issue
Brody
Brody•17mo ago
this was the code i used
import os

current_dir = os.getcwd()

output_dir = "store/random_string"

full_output_dir = os.path.join(current_dir, output_dir)

if not os.path.exists(full_output_dir):
print("path does not exist, creating path")
try:
os.makedirs(full_output_dir, exist_ok=False)
print("no errors creating the path")
except Exception as err:
print("error crating path: ", err)
else:
print("path exists, continuing")

if not os.path.exists(full_output_dir):
print("path does not exist")
else:
print("path exists")
import os

current_dir = os.getcwd()

output_dir = "store/random_string"

full_output_dir = os.path.join(current_dir, output_dir)

if not os.path.exists(full_output_dir):
print("path does not exist, creating path")
try:
os.makedirs(full_output_dir, exist_ok=False)
print("no errors creating the path")
except Exception as err:
print("error crating path: ", err)
else:
print("path exists, continuing")

if not os.path.exists(full_output_dir):
print("path does not exist")
else:
print("path exists")
what directory did you set in this request
ananthankichu
ananthankichuOP•17mo ago
Hey, I found the issue. It was because I was running two services (celery and web ). I was trying to create a new directory inside the celery service. I changed the logic and tried to create the directory in web service and it worked . I don't know why this issue was happening. I tried to find the reason, but I couldn't.
Brody
Brody•17mo ago
happy you could solve it
Want results from more Discord servers?
Add your server