Choosing a path to access a file in Python

One of the possibilities of my project is to give users the opportunity to create their own small .txt file, put it somewhere on their hard drive, which can then be used as criteria for part of my application.

Is there a fixed, common (?) Path for most OS that I could use? Or does anyone have any tips or tricks that can help me?

FYI, during development, I just used a fixed location (/ home / user / Desktop / folder1 / "), to which I then specify the file name to complete the path.

+4
source share
3 answers

os.path.expanduser - a good start - the host ~/ expands to the "user's home directory", which is a computer by a reasonable heuristic for both Unix-y systems and Windows. Of course, you do not want to directly put your file in the house, but a subdirectory of this (which you will need to do if it does not already exist) is reasonable by default.

Of course, allow this parameter to be overridden with the environment variable or command line flag passed to your program, as some platforms are rather scarce as to where, by convention, the application should park such auxiliary data files.

Edit : OP requests an example:

Suppose I wanted this in ~ / tempfoler / and the file transferred to it was args1.txt?

I would suggest finding the exact path through a function such as:

 import errno import os def getfilepath(filename, foldername='tempfoler', envar='MYAPPDIR'): # check for environmental override if envar is not None and envar in os.environ: dirpath = os.environ[envar] else: dirpath = '~/%s' % foldername # expand and ensure it a directory expanded = os.path.expanduser(dirpath) if not os.path.isdir(expanded): if os.path.lexists(expanded): # we're in dire trouble: the path is a file, not a directory! # you could punt to some default value, but I prefer to raise # an exception and allow higher levels to deal with this raise IOError(errno.EEXISTS, "Path is a file, not a dir", expanded) # the following may also raise (permission issues, &c) os.makedirs(expanded) # now the directory exists, the file may or may not (doesn't matter) return os.path.join(expanded, filename) 

There are some design options built in here - they cause exceptions for all kinds of problems (higher levels can catch and process them accordingly, for example, ask the user for an alternative - this lower level function cannot do it right!), Return the path to the file that may or may not exist (thus, it is suitable for reading an existing file or writing a new one), etc.

You can also do fewer checks and rely more on automatic exceptions that occur for all types of anomalies (or vice versa), I just think it puts a reasonable balance (and, of course, it's easy for you to set up if you prefer a slightly different approach to such issues). But the key idea is to always focus on what can go wrong and how to deal with it (often with higher-level code, the only suitable point for a possible user request for alternatives) to make your software reliable.

+7
source

The wx.StandardPaths module contains methods that return various standard locations on the file system and transparently try to "correctly" Unix, Mac OS X, and Windows.

+3
source

For the copy / paste instance there ...

 import os file_obj = open(os.path.expanduser('~/yourapp/file.txt'), 'w') 

Good luck.

0
source

All Articles