Windows 上包含回车符 return 的文件名无法被 Python 识别
Filename containing carriage return on Windows is not recognised by Python
我想select一个文件,但是文件名包含一个回车return所以.isfile()
不断returns False
。当我使用 .fnmatch()
时,它会打印文件名,包括结尾的 回车符 return.
import fnmatch
import os
local_path = 'd:'+os.sep
filename = '1F80813965EDAA4FC5BA44A91E0DBFF1'
local_file = os.path.join(local_path, filename+'\r')
print( os.path.isfile(local_file) )
# Returns False
for file in os.listdir(local_path):
if fnmatch.fnmatch(file, filename+'?'):
print(repr(file))
# Returns 'd:\1F80813965EDAA4FC5BA44A91E0DBFF1\r'
这里有什么问题?是Windows吗?是NTFS分区吗?还是os.path.join()
函数不理解'\r'
?
在您的代码中,以下行创建文件路径。你可以试试去掉里面的\r
local_file = os.path.join(local_path, filename)
Windows 不允许 special characters in filename:
[...]
- Use a backslash (\) to separate the components of a path. The backslash divides the file name from the path to it, and one directory name from another directory name in a path. You cannot use a backslash in the name for the actual file or directory because it is a reserved character that separates the names into components.
[...]
- Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
- The following reserved characters:
- < (less than)
- > greater than)
- : (colon)
- " (double quote)
- / (forward slash)
- \ (backslash)
- | (vertical bar or pipe)
- ? (question mark)
- * (asterisk)
- Integer value zero, sometimes referred to as the ASCII NUL character.
- Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.
- Any other character that the target file system does not allow.
如果您从另一个系统复制了文件,这可能是个问题。如果您需要在 Windows 中使用此文件,您可能需要在复制前重命名它。
我想select一个文件,但是文件名包含一个回车return所以.isfile()
不断returns False
。当我使用 .fnmatch()
时,它会打印文件名,包括结尾的 回车符 return.
import fnmatch
import os
local_path = 'd:'+os.sep
filename = '1F80813965EDAA4FC5BA44A91E0DBFF1'
local_file = os.path.join(local_path, filename+'\r')
print( os.path.isfile(local_file) )
# Returns False
for file in os.listdir(local_path):
if fnmatch.fnmatch(file, filename+'?'):
print(repr(file))
# Returns 'd:\1F80813965EDAA4FC5BA44A91E0DBFF1\r'
这里有什么问题?是Windows吗?是NTFS分区吗?还是os.path.join()
函数不理解'\r'
?
在您的代码中,以下行创建文件路径。你可以试试去掉里面的\r
local_file = os.path.join(local_path, filename)
Windows 不允许 special characters in filename:
[...]
- Use a backslash (\) to separate the components of a path. The backslash divides the file name from the path to it, and one directory name from another directory name in a path. You cannot use a backslash in the name for the actual file or directory because it is a reserved character that separates the names into components.
[...]
- Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
- The following reserved characters:
- < (less than)
- > greater than)
- : (colon)
- " (double quote)
- / (forward slash)
- \ (backslash)
- | (vertical bar or pipe)
- ? (question mark)
- * (asterisk)
- Integer value zero, sometimes referred to as the ASCII NUL character.
- Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.
- Any other character that the target file system does not allow.
如果您从另一个系统复制了文件,这可能是个问题。如果您需要在 Windows 中使用此文件,您可能需要在复制前重命名它。