Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage directory path while developing in windows and testing in linux python api

Tags:

python

I am working on python web app which involve few python flask api. I am developing this on windows and have tested all the api using postman. Everything is working fine. In my webapp project, I have to create few directories if they are not present, so for that I am using below code:

if not os.path.isdir("dataset/" + client_name):
    # if client name directory is not created, then create it
    client_dir = curr_path + '\\' + 'dataset\\' + client_name 
    os.mkdir(client_dir)

I am deploying this webapp on pythonanywhere.com. This uses linux as it platform and I am using windows for development due to which issues are coming. Now in windows we use \ for directory but in linux, it uses /.

How can I manage this while working on windows and deploy on linux. Is there some kind of config I can define.?

Thanks

like image 842
S Andrew Avatar asked Aug 10 '26 06:08

S Andrew


1 Answers

You can avoid using slashes in your code all together. Construct your paths with os.path.join. In the example you posted, all you should have to do is change

client_dir = curr_path + '\\' + 'dataset\\' + client_name

to

client_dir = os.path.join(curr_path, "dataset", client_name)

Edit: You should also change

if not os.path.isdir("dataset/" + client_name):

to

if not os.path.isdir(os.path.join("dataset", client_name))

and the path will be constructed appropriately for whatever system the code is running on.

like image 71
SyntaxVoid supports Monica Avatar answered Aug 13 '26 00:08

SyntaxVoid supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!