A silly question on file opening

Nobody nobody at nowhere.com
Wed Feb 10 22:37:06 EST 2010


On Wed, 10 Feb 2010 21:23:08 +0000, Steven D'Aprano wrote:

> The solution to this is to remember that Windows accepts forward slashes 
> as well as backslashes, and always use the forward slash. So try:
> 
> open("D:/file") 
> 
> and see if that works.

The solution is not to hard-code pathnames in your source file. Read the
base directory from an environment variable, registry key, or
configuration file, and use os.path.join() to construct paths relative to
that directory.

On Unix, there are situations which require hard-coding pathnames, e.g.
the path to the package's system-wide configuration file.

But on Windows, there isn't a single directory whose name is fixed.
"Windows" isn't guaranteed to be on "C:", and the other standard
directories typically have names which vary by language. Hard-coding
"Program Files" is a quick way to guarantee that your software won't work
on systems using a language other than English.




More information about the Python-list mailing list