简体   繁体   中英

How to read/access a filename with non-ascii characters

I have the following file:

$ ls -l /var/tmp/attachment
-rw-rw-r-- 1 3103 king_kong 27136 Apr  7  2014 å·æ°£ç³»çµ±åæ°´é維修.msg

When I try to list the file specifically, I get a file not found error:

$ ls -l /var/tmp/attachment/å·æ°£ç³»çµ±åæ°´é維修.msg
ls: /var/tmp/attachment/å·æ°£ç³»çµ±åæ°´é維修.msg: No such file or directory

Thus, I can't figure out how to rename this file to have a simpler filename (ascii chars only), like "example.msg".

Ideally, I'd like to read this file in Python. This is clearly not the way to do it:

>>> data = open(r'/var/tmp/attachment/å·æ°£ç³»çµ±åæ°´é維修.msg', 'rb')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: '/var/tmp/attachment/\xe5\xb7\xe6\xb0\xa3\xe7\xb3\xbb\xe7\xb5\xb1\xe5\xe6\xb0\xb4\xe9\xe7\xb6\xad\xe4\xbf\xae.msg'

Apart from getting the source to save the file with a different name, is there any way to "fix" the filename via command line in linux, or using Python?

When on the shell, your terminal will be trying to translate the byte string to printable chars. It's possible some of the bytes aren't valid chars at all so copy-pasting them is useless.

The easiest was to deal with non-printable filenames on the shell is to use get a file's inode then use find to do something with it.

To get a file's inode:

ls -il

The first column is the inode. Pass this to find:

find . -inum <inode-number> -exec mv {} newfilename.msg \; 

In Python, the trick is open a file with an odd name is to do a file list and pass the resulting string to open .

For example:

os.listdir('/var/tmp/attachment/')

我有同样的问题尝试

data = open(ru'/var/tmp/attachment/å·æ°£ç³»çµ±åæ°´é維修.msg', 'rb')

这样你有一个扩展名为'.msg'的文件,请尝试

rename 's/.*/test.msg/' *.msg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM