简体   繁体   中英

BASE_DIR outputs correct using print, but in a variable it just outputs C:

If im using print on base_dir = settings.BASE_DIR it outputs everything correctly. However when I use this variable to create a new variable using os.path.join , it just outputs C:

Print example: C:\\Users\\me\\Google Drive\\gitlab\\rootfolder

Example on code where it just outputs C: and the paths after which shows up correct.

blendfile = os.path.join(base_dir, '/var/media', userpathname, newest).replace("\\\\", "/")

Comes out as: C:/var/media/userpathname/newest

Change '/var/media' to var/media' in join function, that is remove the preceeding slash from the second parameter. hence the code should be,

blendfile = os.path.join(base_dir,  userpathname, newest).replace("\\", "/")



EXAMPLE

In [16]: import os                                                                                                                                    

In [17]: BASE = "Users\me\Google Drive\gitlab\rootfolder"                                                                                             

In [18]: append_path__1 = "/var/media" # with preceeding slash                                                                                                                

In [19]: append_path__2 = "var/media"  # without preceeding slash                                                                                                               

In [20]: os.path.join(BASE,append_path__1)                                                                                                            
Out[20]: '/var/media'

In [21]: os.path.join(BASE,append_path__2)                                                                                                            
Out[21]: 'Users\\me\\Google Drive\\gitlab\rootfolder/var/media'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM