file access - Python Creating Unwanted Folder in Directory -
Whenever I call this method, then Python is building a folder in my directory. This method is used by my demo applications Is one of those which requires access to the local area of the server. Def filepath (filename, foldername = '', envar = 'MYAPPDIR'): If there is no envar and envar osenver: dirpath = os.environ [envar] else: dirpath = ' ~ / MyFolder /% s'% folder name expanded = os.path.expanduser (dirpath) if not os.path.isdir (extended): if os.path.lexists (expanded): Increase IOError (errno.EEXIST, "path One file, neither an ", extended) ommescars (extended) returns. Os. Path.join (expanded, filename)
I want to stop it from being
< P> Please Note : Someone within the user default May exist in the directory. Therefore the default folder is myFolder
, though user myOtherFolder
is named under myFolder
(hence ~ / myFolder / MyOtherFolder /
) Then they can do this is the functionality that I am trying to implement, so I have not been used in the folder = ''
method (if I think there is a problem). def filepath (file name, folder name = none, envar = 'MYAPPDIR') : Default = '~ / myFolder' if folder name: default = os.path.join (default, name of folder) try dirpath = os.path.expanduser (os.environ.get (envar, default)): osmakers ( Dirpath) Excluding oSError as e: e.errno! = Errno.EEXIST: returns os.path.join (dirpath, filename)
First of all if you were in the "big" change by removing the "typo"?).
However, it appears that you want to override "~ / myFolder" to envar, not "~ / myfolder / otherSpecifiedByUser", give:
def filepath file Name, folder = none, envar = 'MYAPPDIR'): dirpath = os.path.expanduser (os.environ.get (envar, "~ / myFolder")) if folder name: dirpath = os.p Ath.join ( Dirpath, foldername) Try: osmakers (dirpath) to exclude OSError as e: if e.errno! = Errno.EEXIST: return os.path.join (dirpath, filenam E) Increase
In addition, if you are storing config files, then you can actually follow it, if you actually have something like "~ / .myfolder" Using:
def filepath (filename, fold name = none, anvar = 'mpepdir'): default = OS path.joint (OSX.ET ("XDG_CONFIG_HOME", " Dirpath = os.path.join (dirpath, foldername) try: os. Makedirs (dirpath) oserr as e Except: if e.errno! = Errno.EEXIST: raise return os.path.join (dirpath, filename)
Comments
Post a Comment