Python How do I use '~' (tilde) in the context of paths?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3488603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:20:46  来源:igfitidea点击:

How do I use '~' (tilde) in the context of paths?

pythonpathtilde

提问by captcadaver

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!

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!

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

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('~/path/in/home/area').

EDIT: This is in Python. I fixed the problem, using os.path.expanduser('~/path/in/home/area').

采纳答案by dierre

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

it is your $HOMEvar in UNIX, which usually is /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

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

回答by Blrfl

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

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

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, ~/filewill become /path/to/users/home/directory/file, but ./~/filewill be interpreted literally (i.e., "a file called filein a subdirectory of .called ~").

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, ~/filewill become /path/to/users/home/directory/file, but ./~/filewill be interpreted literally (i.e., "a file called filein a subdirectory of .called ~").

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.

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.

回答by hum3

If you are using pathlib for filenames then you can use on both Windows and Linux (I came here for a windows answer):

If you are using pathlib for filenames then you can use on both Windows and Linux (I came here for a windows answer):

python from pathlib import Path p = Path('~').expanduser() print(p)

python from pathlib import Path p = Path('~').expanduser() print(p)