What should have been done
You should have performed
home=os.path.expanduser("~") first and then
~ is treated as text in python and doesn't expand like it does in
bash or other shells.
$ python -c 'import os;home=os.path.expanduser("~"); os.mkdir(os.path.join(home,"something"))'
$ ls -ld ~/something
drwxrwxr-x 2 xieerqi xieerqi 4096 7月 12 21:00 /home/xieerqi/something/
How to get rid of it
In addition to the already mentioned methods, we can use hex value for
~ from the ascii table of characters, which unlike actual
~ doesn't expand to user's home directory on command line (In case you're wondering, when tilde appears at the beginning of a word, it expands to user's home directory. Compare
ls ~ and
Here's how it would work:
# Make the directory
$ mkdir ./~
$ stat ./$'\x7e'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 801h/2049d Inode: 5768019 Links: 2
Access: (0775/drwxrwxr-x) Uid: ( 1000/ xieerqi) Gid: ( 1000/ xieerqi)
Access: 2017-07-12 21:05:31.382828424 -0600
Modify: 2017-07-12 21:05:31.382828424 -0600
Change: 2017-07-12 21:05:31.382828424 -0600
# and remove it
$ rmdir ./$'\x7e'
$ ls $'\x7e'
ls: cannot access '~': No such file or directory