gpt4 book ai didi

python - 文件大小总和的脚本

转载 作者:行者123 更新时间:2023-11-30 23:13:56 25 4
gpt4 key购买 nike

我正在尝试计算各种文件的大小总和。这是我的脚本:

import os
date = raw_input('Enter date in format YYYYMMDD ')
file1 = 'p_poupe_' + date + '.tar.gz.done'
file2 = 'p_poupw_' + date + '.tar.gz.done'
file3 = 'p_pojk_' + date + '.tar.gz.done'

a1 = os.system('zcat ' + file1 + '|wc --bytes')
a2 = os.system('zcat ' + file2 + '|wc --bytes')
a3 = os.system('zcat ' + file3 + '|wc --bytes')

print a1,a2,a3
sum = a1 + a2 + a3

print sum

但是这些值没有存储在变量中。谁能告诉我我做错了什么。如何修改脚本以便将值存储在变量中而不是作为输出。

最佳答案

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

https://docs.python.org/2/library/os.html#os.system

问题是您使用退出代码而不是标准输出数据作为您的“值”。例如,您可能希望使用 subprocess.Popen 。或者只是通过打开文件手动编写解决方案。

尝试使用https://docs.python.org/3/library/gzip.html

import gzip
def get_fcont_len(fname):
with gzip.open(fname) as f:
return len(f.read())
total = 0
date = raw_input('Enter date in format YYYYMMDD ')
total += get_fcont_len('p_poupe_' + date + '.tar.gz.done')
total += get_fcont_len('p_poupw_' + date + '.tar.gz.done')
total += get_fcont_len('p_pojk_' + date + '.tar.gz.done')
print(total)

关于python - 文件大小总和的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29032362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com