#!/usr/bin/python3.5
import os, sys
from time import strftime
from os.path import join
path = '/home/william/.btrfs-snapshots/'
age = 24
todaytuple = (int(strftime("%y")),int(strftime("%m"))int(strftime("%d")))
def get_date_tuple(filename):
numberstr = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')
filename = ''
datelist = []
global datepart
datepart = ''
for i in filename:
for n in numberstr:
if i == n:
datepart += i
if len(datepart) == 2:
y = int(datepart)
datelist.append(y)
datepart = ''
else:
pass
snapshotbirthtuple = tuple(datelist)
return snapshotbirthtuple
for fname in os.listdir(path):
snapshotbirthtupleinscope = get_date_tuple(fname)
print(snapshotbirthtupleinscope)
if snapshotbirthtupleinscope[0] <= todaytuple[0]:
print(snapshotbirthtupleinscope)
打印()
只是空括号,
我期待一对数字的元组。我很确定该函数提供了正确的输出,但我想我可能没有正确地从函数中传递返回变量。
() #the tuple returning nothing
Traceback (most recent call last):
File "./snapshotfinal.py", line 38, in <module>
if snapshotbirthtupleinscope[0] <= todaytuple[0]:
IndexError: tuple index out of range
为什么是snapshotbirthtupleinscope!= snapshotbirthtuple?
答案 0 :(得分:0)
看起来你可以使用一些工作。首先根据您的变量名称,您需要为filename分配一些内容,例如:
filename = r"C:\MyFile.txt"
完成后,您将需要打开文件并解析其内容:
with open( filename, "r") as fp:
data = fp.read()
# Process Data In File where datelist is set
# end with automatically closes file for you
snapshotbirthtuple = tuple(datelist)
return snapshotbirthtuple
原始代码没有回答的原因是因为行:
for i in filename:
filename是一个空字符串,没有任何内容可以迭代,因此永远不会为i分配任何值。因此,您的循环完全被跳过,因此日期列表永远不会被分配