如何在路径上下文中使用'〜'(代字号)?

时间:2022-06-01 11:47:32

I'm a web application development noob. I have a function that opens a file and reads it. Unfortunately, the directory structures between the test and production servers differ. I was told to "use a path relative to ~". I haven't been able to find any resources on the '~', though!

我是一个Web应用程序开发菜鸟。我有一个打开文件并读取它的函数。不幸的是,测试服务器和生产服务器之间的目录结构不同。我被告知“使用相对于〜的路径”。但是,我无法在'〜'上找到任何资源!

How do I use the tilde character in the context of paths?

如何在路径上下文中使用波形符?

EDIT: This is in Python. I fixed the problem, using os.path.expanduser().

编辑:这是在Python中。我使用os.path.expanduser()修复了问题。

2 个解决方案

#1


34  

it is your $HOME var in UNIX, which usually is /home/username.

它是UNIX中的$ HOME var,通常是/ home / username。

"Your home" meaning the home of the user who's executing a command like cd ~/MyDocuments/ is cd /home/user_executing_cd_commnd/MyDocuments

“你的家”意味着正在执行像cd~ / MyDocuments /这样的命令的用户的家是cd / home / user_executing_cd_commnd / MyDocuments

#2


14  

Unless you're writing a shell script or using some other language that knows to substitute the value of $HOME for ~, tildes in file paths have no special meaning and will be treated as any other non-special character.

除非您正在编写shell脚本或使用其他语言知道将$ HOME的值替换为〜,否则文件路径中的波形符号没有特殊含义,将被视为任何其他非特殊字符。

If you are writing a shell script, shells don't interpret tildes unless they occur as the first character in an argument. In other words, ~/file will become /path/to/users/home/directory/file, but ./~/file will be interpreted literally (i.e., "a file called file in a subdirectory of . called ~").

如果您正在编写shell脚本,则shell不会解释tildes,除非它们作为参数中的第一个字符出现。换句话说,〜/ file将成为/ path / to / users / home / directory / file,但是./~/file将按字面解释(即,“。在〜的子目录中称为文件的文件”)。

Used in URLs, interpretation of the tilde as a shorthand for a user's home directory (e.g., http://www.foo.org/~bob) is a convention borrowed from Unix. Implementation is entirely server-specific, so you'd need to check the documentation for your web server to see if it has any special meaning.

在URL中使用,将波浪号解释为用户主目录的简写(例如,http://www.foo.org/~bob)是从Unix借用的约定。实现完全是服务器特定的,因此您需要检查Web服务器的文档,看它是否有任何特殊含义。

#1


34  

it is your $HOME var in UNIX, which usually is /home/username.

它是UNIX中的$ HOME var,通常是/ home / username。

"Your home" meaning the home of the user who's executing a command like cd ~/MyDocuments/ is cd /home/user_executing_cd_commnd/MyDocuments

“你的家”意味着正在执行像cd~ / MyDocuments /这样的命令的用户的家是cd / home / user_executing_cd_commnd / MyDocuments

#2


14  

Unless you're writing a shell script or using some other language that knows to substitute the value of $HOME for ~, tildes in file paths have no special meaning and will be treated as any other non-special character.

除非您正在编写shell脚本或使用其他语言知道将$ HOME的值替换为〜,否则文件路径中的波形符号没有特殊含义,将被视为任何其他非特殊字符。

If you are writing a shell script, shells don't interpret tildes unless they occur as the first character in an argument. In other words, ~/file will become /path/to/users/home/directory/file, but ./~/file will be interpreted literally (i.e., "a file called file in a subdirectory of . called ~").

如果您正在编写shell脚本,则shell不会解释tildes,除非它们作为参数中的第一个字符出现。换句话说,〜/ file将成为/ path / to / users / home / directory / file,但是./~/file将按字面解释(即,“。在〜的子目录中称为文件的文件”)。

Used in URLs, interpretation of the tilde as a shorthand for a user's home directory (e.g., http://www.foo.org/~bob) is a convention borrowed from Unix. Implementation is entirely server-specific, so you'd need to check the documentation for your web server to see if it has any special meaning.

在URL中使用,将波浪号解释为用户主目录的简写(例如,http://www.foo.org/~bob)是从Unix借用的约定。实现完全是服务器特定的,因此您需要检查Web服务器的文档,看它是否有任何特殊含义。