Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

path

tilde

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?

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

like image 407
captcadaver Avatar asked Aug 15 '10 18:08

captcadaver


People also ask

Can you use '~' in path?

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.

How do you put a tilde in a path?

~/ (tilde slash) The tilde (~) is a Linux "shortcut" to denote a user's home directory. Thus tilde slash (~/) is the beginning of a path to a file or directory below the user's home directory. For example, for user01, file /home/user01/test. file can also be denoted by ~/test.

What is tilde in Windows path?

Once you exceed 255 characters, the operating system must remove characters from the file path and replace them with tilde (~) in order to make the folder path fit on your computer. This process of replacing characters with tilde is called Truncation.


2 Answers

it is your $HOME var 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

like image 120
dierre Avatar answered Sep 27 '22 19:09

dierre


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.

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 ~").

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.

like image 35
Blrfl Avatar answered Sep 27 '22 18:09

Blrfl