如何测试两个字符串是否指向同一个文件或目录? [英] How to test if two strings point to the same file or directory?

查看:56
本文介绍了如何测试两个字符串是否指向同一个文件或目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将文件系统路径作为字符串进行比较非常脆弱。是否有一个更好的方法来测试两个路径是否指向同一个文件或目录

(这将跨平台工作?)


谢谢,

-Sandra

Comparing file system paths as strings is very brittle. Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)

Thanks,
-Sandra

推荐答案

2006年12月16日星期六17:02 :04 - 0800,Sandra-24写道:
On Sat, 16 Dec 2006 17:02:04 -0800, Sandra-24 wrote:

比较文件系统路径,因为字符串非常脆弱。
Comparing file system paths as strings is very brittle.



为什么这么说?你在想这样的事吗?


/home//user/somedirectory/../file

/ home / user / file


两者都指向同一个文件。

Why do you say that? Are you thinking of something like this?

/home//user/somedirectory/../file
/home/user/file

Both point to the same file.


是否有更好的方法来测试两条路径是否指向到同一个文件或目录

(这会跨平台工作吗?)
Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)



你想要获得多么复杂?如果您正在考虑别名,

硬链接,快捷方式,SMB共享和其他复杂情况,如果有一个简单的方法我将会感到惊讶。


但是对于上面的简单案例:

How complicated do you want to get? If you are thinking about aliases,
hard links, shortcuts, SMB shares and other complications, I''d be
surprised if there is a simple way.

But for the simple case above:


> > import os
path =''/ home//user/somedirectory /../ file''
os.path.normpath(path)
>>import os
path = ''/home//user/somedirectory/../file''
os.path.normpath(path)



''/ home / user / file''


-

史蒂文。

''/home/user/file''

--
Steven.


将文件系统路径作为字符串进行比较是非常脆弱的。是否有一个
Comparing file system paths as strings is very brittle. Is there a

更好的方法来测试两个路径是否指向同一个文件或目录

(这将跨平台工作?)
better way to test if two paths point to the same file or directory
(and that will work across platforms?)



os.path.samefile(filename1,filename2)

os.path.sameopenfile(fileobject1,fileobject2)


-tkc

os.path.samefile(filename1, filename2)
os.path.sameopenfile(fileobject1, fileobject2)

-tkc




Tim Chase写道:

Tim Chase wrote:

将文件系统路径作为字符串进行比较是非常脆弱的。是否有一个更好的方法来测试两个路径是否指向同一个文件或目录

(这将跨平台工作?)
Comparing file system paths as strings is very brittle. Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)



os.path.samefile(filename1,filename2)

os.path.sameopenfile(fileobject1,fileobject2)


os.path.samefile(filename1, filename2)
os.path.sameopenfile(fileobject1, fileobject2)



很好的尝试,但他们不跨平台工作。

Nice try, but they don''t "work across platforms".


这篇关于如何测试两个字符串是否指向同一个文件或目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆