Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在尝试从字典中查找以关键字名称开头的任何文件_Python_Python 2.7_Dictionary - Fatal编程技术网

Python 正在尝试从字典中查找以关键字名称开头的任何文件

Python 正在尝试从字典中查找以关键字名称开头的任何文件,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,正在尝试从字典中查找以关键字名称开头的任何文件。当前代码: # Example of student dictionary students = {'lastName-firstName': 'mum_email@optusnet.com.au', 'jones-bob': 'bob-jones@gmail.com', 'doe-john': 'dads_email@bigpond.net.au'} def files_to_attach(): atta

正在尝试从字典中查找以关键字名称开头的任何文件。当前代码:

# Example of student dictionary
students = {'lastName-firstName': 'mum_email@optusnet.com.au', 'jones-bob': 'bob-jones@gmail.com', 'doe-john': 'dads_email@bigpond.net.au'}                

def files_to_attach():
    attachments = ['/Users/home/Downloads/Scale score ACER  copy.png']
    # Put file names from directory into a list
    # Example of files in list - f = ['lastName-firstName-pat-maths-plus-test-9-2017-03-22-10-23.png',
    #                                'lastName-firstName-pat-r-comprehension-test-9-2017-03-24-12-56.png',
    #                                'etc...']
    f = []
    for filenames in os.walk('/Users/home/Downloads/ACER-Results'):
        f.extend(filenames)
    # Find any files with student's name
    for key, value in students.iteritems():
        # UNSURE IF I NEED TO DO THIS???
        for files_to_attach in f:
            # If the start of the student file begins with the students key...
            if files_to_attach.startswith(key):
                # Add it to the attachments list.
                attachments.append(files_to_attach)
        # Sends email to parent with student's results attached
        send_mail(send_from, value, subject, text, attachments=[])
获取此错误:

File "test.py", line 29, in files_to_attach
    if files_to_attach.startswith(key):
AttributeError: 'list' object has no attribute 'startswith'

是否需要使用正则表达式(re)来搜索文件?

os.walk
返回目录中
(根、目录、文件)的元组。在代码中

for filenanmes in os.walk(...):
    f.extend(filenames)
您正在使用元组扩展列表
f
,因此
f
的最终结果将是元组列表。稍后,当您在中提取列表的内容时

for files_to_attach in f:
    if files_to_attach.startswith(key):
        ...
这里的
文件\u to\u attach
将是一个元组。您应该做的是正确提取第一个for循环中元组的内容:

for root, dirs, files in os.walk(...):
    for fi in files:
        f.append(fi)
或另一种选择:

for fi in os.listdir(...):
    if os.path.isfile(fi): # Get the correct path to fi
        f.append(fi)

用于os.walk中的文件名
文件名
是元组根/dirs/files。检查
os.walk
文档。亲爱的@Carles Mitjans,我已经不再收到错误,谢谢。但它似乎没有将文件附加到“attachments”变量。你知道那里可能出了什么问题吗?@Phil这意味着附件返回False之前的
if
语句。尝试在
if
语句之前使用一些
print
语句进行调试,以查看要附加的文件包含哪些内容。当我在f:
中的
for files\u to\u attach下运行
print files\u to\u attach
时,它会一次打印一个字母的文件名,而不是整个文件名?@Phil,因为您是
extend
ing您的
f
列表。这将一次添加每个字符。在第一个循环中使用
append
(我也编辑了我的答案),我还有很多调试要做!!非常感谢您的帮助,非常感谢。