且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是存储文件路径的完美(独立于操作系统)方式?

更新时间:2023-10-09 21:02:10

10月19日,8:35 * am,Stef Mientki< stef.mien ... @ gmail.comwrote:

我(再次)想知道什么是完美的存储方式,独立于操作系统,

文件路径?



我不认为有这样的事情。你试图解决什么问题?

解决?


On Sun,2008年10月19日14:35:01 +0200,Stef Mientki写道:

你好,


我(再次)想知道什么是完美的存储方式,独立于操作系统,

文件路径?



完美?我无法想象任何可以在每个可以想象的

操作系统上工作的方案,无论是现在还是将来。


然而,在实践中我认为有两个常用表单仍在使用中:

Posix路径和Windows路径。我相信OS / 2可以处理Windows

路径名,Mac OS X使用Posix路径(我认为......)。如果你必须支持经典Mac OS或其他非Posix系统,那么你的生活将变得有趣和复杂。


并且我们甚至不考虑Unicode问题...


您可能会发现此页面很有用:
http://en.wikipedia.org/wiki/Path_(计算


请注意,原始字符串用于正则表达式,而不是Windows路径.Raw

字符串不能以反斜杠结尾,所以你不能这样做:
>
r''C:\我的文件\''


相反,你可以通过利用来避免逃避反斜杠

事实上Windows会接受正斜杠以及
反斜杠作为路径分隔符,而是写''C:/ My Documents /'。


我假设您熟悉os.path中的路径操作实用程序?


>> import os
os.path.splitdrive(''C:// My Documents / My File.txt'')



(''C:\\\\''',' '我的文件\\My File.txt'')


我不得不伪造上面的输出,因为我没有运行Windows,所以请原谅
$ b如果我弄错了,请告诉我。


但老实说,我认为你最大的问题是找不到平台 -

独立存储路径的方式,但只是简单地在每个

操作系统关于文件存储位置的约定之间进行转换。


在Linux中,配置文件应该进入:


~ /。< appname> /或/ etc /< appname> /


在Windows(哪个版本?)中应该进入文件和

设置文件夹,无论如何。


没有单一的条纹这可以代表这两个惯例!


-

史蒂文


Steven D' 'Aprano< st *** @ REMOVE-THIS-cybersource.com.auwrote:

在Linux中,配置文件应该进入:


~ /。< appname> /或/ etc /< appname> /


在Windows中(哪些版本?)然后应该进入文档和

设置文件夹,无论如何。


没有单个字符串可以代表这两种约定!



第一个应该适用于Linux和Windows:


>> os.path.normpath(os.path.expanduser(''〜/ .appname''))



''C:\\Documents and Settings \\Duncan \\ .appname''


hello,

I (again) wonder what''s the perfect way to store, OS-independent,
filepaths ?
I can think of something like:
- use a relative path if drive is identical to the application (I''m
still a Windows guy)
- use some kind of OS-dependent translation table if on another drive
- use ? if on a network drive

I''m interested what you all use for this kind of problem.
And I wonder why there isn''t a standard solution / library in Python
available.

thanks,
Stef Mientki

On Oct 19, 8:35*am, Stef Mientki <stef.mien...@gmail.comwrote:
I (again) wonder what''s the perfect way to store, OS-independent,
filepaths ?

I don''t think there is any such thing. What problem are you trying to
solve?


On Sun, 19 Oct 2008 14:35:01 +0200, Stef Mientki wrote:
hello,

I (again) wonder what''s the perfect way to store, OS-independent,
filepaths ?

"Perfect"? I can''t imagine any scheme which will work on every imaginable
OS, past present and future.

However, in practice I think there are two common forms still in use:
Posix paths, and Windows paths. I believe that OS/2 can deal with Windows
pathnames, and Mac OS X uses Posix paths (I think...). If you have to
support Classic Mac OS or other non-Posix systems, then your life will
become interesting and complicated.

And let''s not even consider Unicode issues...

You might find this page useful:
http://en.wikipedia.org/wiki/Path_(computing)

Note that raw strings are for regular expressions, not Windows paths. Raw
strings can''t end in a backslash, so you can''t do this:

r''C:\My Documents\''

Instead, you can avoid having to escape backslashes by taking advantage
of the fact that Windows will accept forward slashes as well as
backslashes as path separators, and write ''C:/My Documents/'' instead.

I assume you''re familiar with the path-manipulation utilities in os.path?

>>import os
os.path.splitdrive(''C://My Documents/My File.txt'')

(''C:\\\\'', ''My Documents\\My File.txt'')

I had to fake the above output because I''m not running Windows, so excuse
me if I got it wrong.

But honestly, I think your biggest problem isn''t finding a platform-
independent way of storing paths, but simply translating between each
OS''s conventions on where files should be stored.

In Linux, config files should go into:

~/.<appname>/ or /etc/<appname>/

In Windows (which versions?) then should go into the Documents And
Settings folder, where ever that is.

There''s no single string which can represent both of these conventions!

--
Steven


Steven D''Aprano <st***@REMOVE-THIS-cybersource.com.auwrote:
In Linux, config files should go into:

~/.<appname>/ or /etc/<appname>/

In Windows (which versions?) then should go into the Documents And
Settings folder, where ever that is.

There''s no single string which can represent both of these conventions!

The first of those should do nicely for both Linux and Windows:

>>os.path.normpath(os.path.expanduser(''~/.appname''))

''C:\\Documents and Settings\\Duncan\\.appname''