首页 > 解决方案 > 在 Python 中将包含多个字符串的变量写入文本文件

问题描述

我正在尝试创建一个将用作更大脚本的一部分的函数。我希望此函数对当前工作目录中的所有文件进行哈希处理,并将结果作为文本文件输出到同一目录。这就是我想出的:

def input_hash (in_file):
    print("All the input files have been hashed using the sha1 checksum \n")
    cwd = os.getcwd()                   
    for video in os.listdir(cwd):       
        with open(video, 'rb') as getsha1:
            data = getsha1.read()
        checksum = hashlib.sha1(data).hexdigest()
        filenames = os.path.join(video, checksum)
        print(filenames)                    #returns filenames as str
        output = open("Input_hash.txt", "w+")
        output.writelines([i + '\n' for i in filenames])
        output.close()

当我在我的测试文件上运行这个函数时,“filenames”变量中存储了以下信息,每个项目都是一个字符串:

所有输入文件都已使用 sha1 校验和进行哈希处理

IMG_0020.MOV\35bcfab1df29bc20113420683a81982f0c731012 IMG_0063.MOV\251eb1376d6d2fff047244192c4cdaf88b251e92 IMG_0065.MOV\2cce5734c72b00e608ff511dafabf953167bc5c5 IMG_0074.MOV\4bcada3d839e06a477ecd3b1eba8a75ce21e8e0f IMG_0076.MOV\46d7d619780f12eb22c088ff0da505f10ecaa92f IMG_0087.MOV\b89fbc6a1a949ff7f13ac0ac09d10518c3961abe IMG_0113.MOV\0e42993b3cd8261ac869cd8947157e5936fb52dd IMG_0120.MOV\c9c0eea3b4c00eeca1a1cb218564a01db2ae7ed9 IMG_0242.MOV\82d8bb859396b0b70c4cb1ff265daa8a82db2143 IMG_0247.MOV\d2a9291ca26df3f03cbae5ec115f08f6084a906a

这正是我想要的;但是,写入 txt 文件的唯一输出是该变量的最后一行,它是垂直写入的,而不是水平写入的。

因此,我试图将“文件名”变量的所有内容完全按照此处显示的方式写入文本,但我遇到了障碍。

我已经尝试了所有这些解决方案

似乎没有一个工作。非常感谢任何建议或帮助。

@破冰船454

我的格式不正确吗?

def input_hash (in_file):
print("All the input files have been hashed using the sha1 checksum ")
cwd = os.getcwd()                   #gets current directory
for video in os.listdir(cwd):       #gets all the files in the directory
    with open(video, 'rb') as getsha1:
        data = getsha1.read()
    checksum = hashlib.sha1(data).hexdigest()
    filenames = []
    for video in os.listdir(cwd):
        filenames.append(os.path.join(video, checksum))
    with open("output.txt", "w+") as output:
        output.writelines([name + "\n" for name in filenames])
    output.close()

解决方案:

def input_hash (in_file):
    print("All the input files have been hashed using the sha1 checksum ")
    cwd = os.getcwd()                  
    filenames = []
    for video in os.listdir(cwd):
        with open(video, 'rb') as getsha1:
            data = getsha1.read()
        checksum = hashlib.sha1(data).hexdigest()
        filenames.append(os.path.join(video, checksum))
    with open("output.txt", "w+") as output:
        output.writelines([name + "\n" for name in filenames])
    output.close()

标签: python-3.x

解决方案


原因实际上是您尝试将 LAST 文件名 + 哈希写入文件。在您的脚本中,您始终当前文件名路径 + hashsum 分配给变量filenames。最后,您将其设置为最后一个文件名 + hashsum。

由于您的 lastfilename是一个字符串,因此可以对其进行迭代,而不会出现错误:

output.writelines([i + '\n' for i in filenames])

所以,拥有你的 last filename = "IMG_0247.MOV\d2a9291ca26df3f03cbae5ec115f08f6084a906a"[i + '\n' for i in filenames]将会变成["I", "\n", "M", "\n", "G", ...],这正是你看到它垂直书写的原因

要解决这个问题,您应该简单地将每个文件名附加到一个数组中,并在最后写出来

filenames = []
for video in os.listdir(cwd):
   with open(video, 'rb') as getsha1:
       data = getsha1.read()
   checksum = hashlib.sha1(data).hexdigest()
   filenames.append(os.path.join(video, checksum))

with open("output.txt", "w+") as output:
    output.writelines([name + "\n" for name in filenames])

推荐阅读