[英]Training '\x00' s and TypeError: stat() argument 1 must be encoded string without null bytes, not str
I have a json file where I stored path to several files like this:我有一个 json 文件,我在其中存储了几个文件的路径,如下所示:
>> head test_data.json
data/Benign/P_00004_LEFT_CC.tif
data/Benign/P_00004_LEFT_MLO.tif
data/Benign/P_00004_RIGHT_MLO.tif
data/Benign/P_00018_RIGHT_CC.tif
Now the problem is when I read the file in python and save each line in an array I have trailing \\x00\\x00\\x00\\x00\\x00
at the end of line.现在的问题是当我在 python 中读取文件并将每一行保存在一个数组中时,我在行尾有尾随\\x00\\x00\\x00\\x00\\x00
。 I tried str.rstrip()
and str.rstrip('\\n')
but had no luck.我试过str.rstrip()
和str.rstrip('\\n')
但没有运气。
This is causing problem when I try to check the existence of paths using os.path.exists()
like following当我尝试使用os.path.exists()
检查路径是否存在时,这会导致问题,如下所示
if os.path.exists(path_list[0]):
img1 = self.loader(path_list[0])
........
I am getting error at os.path.exists()
我在os.path.exists()
出现错误
TypeError: stat() argument 1 must be encoded string without null bytes, not str
How can I resolve this issue?我该如何解决这个问题?
You can use rstrip('\\x00')
, or you can use rstrip('\\x00\\n')
to strip both '\\n'
and \\x00'
concurrently:您可以使用rstrip('\\x00')
,也可以使用rstrip('\\x00\\n')
同时剥离'\\n'
和\\x00'
:
>>> s = 'data/Benign/P_00004_LEFT_CC.tif\x00\x00\x00\x00\x00\n'
>>> s.rstrip('\n\x00')
'data/Benign/P_00004_LEFT_CC.tif'
That being said, I would ask myself how NULL-characters occur in the file in the first place.话虽如此,我首先会问自己文件中是如何出现 NULL 字符的。
You can process the list of path_list
in batch by using:您可以使用以下命令批量处理path_list
的列表:
path_list = [item.rstrip('\n\x00') for item in path_list]
before using the path_list
when processing the file names.在处理文件名时使用path_list
之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.