Handle invalid characters from filename in unix
try this:
cat a\!aapoorv.txt
or this
cat 'a!aapoorv.txt'
Note that while
cat a\!aapoorv.txt
works in all shells that implement that csh-style history expansion,cat 'a!aapoorv.txt'
doesn't work in csh/tcsh.
for more information, you can see man bash
about the QUOTING.
Here is some of that document:
Quoting is used to remove the special meaning of certain characters or words to the shell.
Quoting can be used to disable spe‐cial treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expan‐sion.
And here is the output:
[kevin@Arch test]$ lsa!aapoorv.txt[kevin@Arch test]$ cat a\!aapoorv.txt Hello, This is a test[kevin@Arch test]$ cat 'a!aapoorv.txt'Hello, This is a test
On Python, you don't need to escape the special character, here is a test:
>>> with open('a!aapoorv.txt') as f:... f.read()... ... 'Hello, This is a test\n'>>> with open(r'a!aapoorv.txt') as f:... f.read()... ... 'Hello, This is a test\n'>>>
use singlequotes ' ':
$ cat 'a!aapoorv.txt'cat: a!aapoorv.txt: No such file or directory
For Bash, you need to use the escaping methods (single quotes or backslash) described by the other answers.
In Python, you shouldn't need to use raw strings or any other type of escaping to open a file with special chars.
For example, this works fine:
my_f_contents = open("a!aapoorv.txt").read()