質問

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/')

他のヒント

I had this same problem try

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

Such you have one file with '.msg' extension ,try

rename 's/.*/test.msg/' *.msg
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top