python - 检查迭代中的所有项目是否具有相同的属性
python - check if all items in an iteration have equal properties
我是 python 的新手,我正在编写一个脚本来登录 ftp 服务器以获取文件列表,然后检查是否所有文件都在同一个目录中文件夹具有相同的大小,但是当文件夹路径相同时我找不到比较大小的方法,这是我到目前为止的位置:
import ftputil
server = 'xxxxxx'
user = 'xxxxxx'
password = 'xxxxxx'
root_folder = 'xxxxxx'
ftp = ftputil.FTPHost(server, user, password)
recursive = ftp.walk(root_folder, topdown=True, onerror=None)
for root_folder, subdir, files in recursive:
for name in files:
file_path = ftp.path.join(root_folder)
file_size = ftp.stat(ftp.path.join(root_folder, name))[6]
print(file_path, file_size)
谁能帮我弄清楚如何比较同一目录中文件的大小?
假设您的大部分代码都有效。我假设您想要 True 或 False,并且当您找到第一个差异时,您想要停止迭代。例如:
same_size = None
common_size = False # initialise
recursive = ftp.walk(root_folder, topdown=True, onerror=None)
for root_folder, subdir, files in recursive:
for name in files:
file_path = ftp.path.join(root_folder)
file_size = ftp.stat(ftp.path.join(root_folder, name))[6]
print(file_path, file_size)
if common_size is None: # execute on first file
common_size = file_size
elif common_size == file_size:
same_size = True
else
same_size = False
break
如果那里只有一个文件,您需要决定要做什么。
从空值开始迭代,将其设置在第一项,然后与该值进行比较。
expected_size = None
for root_folder, subdir, files in recursive:
for name in files:
file_size = ftp.stat(ftp.path.join(root_folder, name))[6]
if expected_size is None:
expected_size = file_size
if expected_size != file_size:
return True
return False
我是 python 的新手,我正在编写一个脚本来登录 ftp 服务器以获取文件列表,然后检查是否所有文件都在同一个目录中文件夹具有相同的大小,但是当文件夹路径相同时我找不到比较大小的方法,这是我到目前为止的位置:
import ftputil
server = 'xxxxxx'
user = 'xxxxxx'
password = 'xxxxxx'
root_folder = 'xxxxxx'
ftp = ftputil.FTPHost(server, user, password)
recursive = ftp.walk(root_folder, topdown=True, onerror=None)
for root_folder, subdir, files in recursive:
for name in files:
file_path = ftp.path.join(root_folder)
file_size = ftp.stat(ftp.path.join(root_folder, name))[6]
print(file_path, file_size)
谁能帮我弄清楚如何比较同一目录中文件的大小?
假设您的大部分代码都有效。我假设您想要 True 或 False,并且当您找到第一个差异时,您想要停止迭代。例如:
same_size = None
common_size = False # initialise
recursive = ftp.walk(root_folder, topdown=True, onerror=None)
for root_folder, subdir, files in recursive:
for name in files:
file_path = ftp.path.join(root_folder)
file_size = ftp.stat(ftp.path.join(root_folder, name))[6]
print(file_path, file_size)
if common_size is None: # execute on first file
common_size = file_size
elif common_size == file_size:
same_size = True
else
same_size = False
break
如果那里只有一个文件,您需要决定要做什么。
从空值开始迭代,将其设置在第一项,然后与该值进行比较。
expected_size = None
for root_folder, subdir, files in recursive:
for name in files:
file_size = ftp.stat(ftp.path.join(root_folder, name))[6]
if expected_size is None:
expected_size = file_size
if expected_size != file_size:
return True
return False