嘿,我需要帮助来处理仿真数据
我正在构建一个函数来检查给定路径中命名文件夹的最高4位数字,以及是否包含名为“ shearrate”的文件。这是在路径本身的目录中以及给定路径中每个未命名为4位的文件夹中完成的。
在我添加检查文件是否存在之前,该功能可以正常工作。
如果能帮助您,将不胜感激:)
def file_filter (folder):
if folder.isdigit() and len(folder) == 4:
return True
def scan_directory(path, name):
folderlist = []
folder_level_1 = 0
folder_level_2 = 0
for folder in os.listdir(path):
if os.path.isdir(os.path.join(path, folder)):
if file_filter(folder) is True:
a_file = Path(os.path.join(path, folder, name))
if a_file.is_file():
if int(folder) > int(folder_level_1):
folder_level_1 = folder
else:
for folder2 in os.listdir(os.path.join(path, folder)):
if file_filter(folder2) is True:
a_file = Path(os.path.join(path, folder, name))
if a_file.is_file():
if int(folder2) > int(folder_level_2):
folder_level_2 = folder2
if file_filter(folder_level_2) is True:
folderlist.append(os.path.join(path, folder, folder_level_2))
folder_level_2 = str(0)
folderlist.append(os.path.join(path, folder_level_1))
return folderlist
folderlist = scan_directory(path, "shearrate")
我得到的错误:
Traceback (most recent call last):
File "data_evaluation.py", line 141, in <module>
search_files("shearrate", path)
File "data_evaluation.py", line 107, in search_files
folderlist = scan_directory(path, name)
File "data_evaluation.py", line 98, in scan_directory
folderlist.append(os.path.join(path, folder_level_1))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/posixpath.py", line 94, in join
genericpath._check_arg_types('join', a, *p)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/genericpath.py", line 149, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'int'
`
答案 0 :(得分:0)
由于错误指出join不能采用int,因此您需要将输入变量的类型改为str:
folderlist.append(os.path.join(path, str(folder_level_1)))
答案 1 :(得分:0)
据我所知,您在调用需要int
参数的join
之前将文件夹转换为string
。在错误消息中这样说。我建议您在调用os.path.join
之前先打印出该变量的值。两种方法都可以强制转换回字符串str(folder_level_1)
。