在检查文件大小(以 MB
为单位)时,我使用 Python 收到语法错误。
我在 if.... 语句
中遇到语法错误。
def createfile(request):
param = request.POST.get('param')
file_info = os.stat(param)
result = convert_bytes(file_info.st_size)
if result > 1 'MB' :
return render(request, 'plant/status.html',
{'message': "File size should be within 1 mb."})
def convert_bytes(num):
""" This function is used for measure file size """
for xe in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
最佳答案
如果结果> 1 'MB':
就是你的问题所在。您正在将 result
与整数 1 进行比较,并且紧接着有一个字符串。也许您的意思是'1 MB'
。这仍然对您没有帮助,因为通过“>”运算符比较字符串实际上不起作用。将其与整数进行比较,无论是 1 还是 1000(因为以千字节为单位)。尝试类似的事情。
关于python - 检查文件大小时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45102675/