如何在python中将.txt文件从列表保存到多个路径

2024-04-26 15:09:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一个简短的代码,这将允许我写一个树莓圆周率和日期/时间序列号到一个文本文件。这些信息将写入一个U盘每次它插入我的树莓皮。这个脚本将使用在多个不同的USB棒覆盆子圆周率,所以我一直试图尽可能一般,因为路径变化等硬件之间的块。你知道吗

我剧本的第一部分很好:

    #import packages
    from datetime import datetime
    import os
    import os.path
    from shutil import copyfile

    usblist = [x for x in usblist if not x.startswith('SETTINGS')] #For some reason I have some folders starting with SETTINGS that I don't want to delete but simply ignore in my list

    #create a new list with the paths for each USB
    usbpathlist = os.path.abspath(usblist[0]),os.path.abspath(usblist[1]) #I will only have two USB sticks inserted at a time

    #for some reason my paths show as in the /home/ directory when it appears that they are mounted in /media/
    usbpathlist = [w.replace('home', 'media') for w in usbpathlist] #this fixes the paths

然后我提取Raspberry Pi和日期/时间所特有的信息,并将它们保存为变量

    def getserial():
        #Extract the serial number from the cpuinfo file
        cpuserial = "0000000000000000"
        try:
            f = open("/proc/cpuinfo", "r")
            for line in f.readlines():
                if line[0:6] == "Serial":
                    cpuserial = line[10:26]
                    f.close()
        except:
            cpuserial = "ERROR00000000000"
        return cpuserial

    rpi_serial = getserial()

    time_experiment = str(datetime.now())

现在我把这些信息写成一个文本文件

    with open("rpi_information.txt", "w") as rpi_information:
        rpi_information.write("Raspberry Pi Serial Number: " + rpi_serial + "\n") #Write the serial number
        rpi_information.write("Time written: " + time_experiment + "\n") #Write the date
        rpi_information.close()

最后,我的脚本中有问题的部分是,我试图将这个文件保存到我的U盘路径中,我在上面保存为“usbpathlist”

    for d in usbpathlist:
        copyfile(rpi_information, d)

从这里我的问题所在-我花了相当多的时间谷歌搜索这个网站,但我一直无法找出如何保存这个文件到每个U盘创建后。网上的很多信息表明,我实际上无法同时将文本文件保存到多个位置,但这似乎不太可能。如果我手动在“rpi”前面插入路径_信息.txt“在上面,它保存而没有问题。我的问题是,因为我的U盘将是不同的,这些路径将改变,所以我不认为这是一个好的解决方案。我从脚本的最后一部分收到的错误(可能是预期的)是:

    Traceback (most recent call last):
      File "home/pi/test.py", line 55 in <module> #test.py is my filename
        copyfile(rpi_information, d)
      File "/usr/lib/python3.5/shutil.py", line 103, in copyfile
        if _samefile(src,dst):
      File "/usr/lib/python3.5/shutil.py", line 88, in samefile
        return os.path.samefile(src,dst)
      File "/usr/lib/python3.5/genericpath.py", line 90, in samefile
        s1 = os.stat(f1)
    TypeError: argument should be string, bytes, or integer, not _io.TextWrapper

最后,我想找到一种方法来传递这个文件到两个U盘没有问题。我想把这些信息作为文本文件保存,因为我以后要在另一台计算机上引用它。任何洞察如何使这项工作将不胜感激或任何意见,如何改善我的代码也将不胜感激。这是我的第一个python脚本,也是我关于StackOverFlow的第一个问题(尽管我已经潜伏了很多年),所以请对我放轻松!我也读了很多关于这个问题的文档,所以我很困惑。提前谢谢你的帮助。你知道吗

---编辑--- 下面的用户Vulcan建议我的错误是在copyfile函数中包含“.txt”失败。我以前也试过,但也没用。不幸的是,这会产生错误:

    Traceback (most recent call last):
      File "home/pi/test.py", line 55 in <module> #test.py is my filename
        copyfile(rpi_information.txt, d)
    AttributeError: '_io.TextWrapper' object has no attribute 'txt'

Tags: theinpyimporttxt信息forinformation
1条回答
网友
1楼 · 发布于 2024-04-26 15:09:12

错误跟踪在这里突出了您的问题:argument should be string... not _io.TextWrapper。调用copyfile(rpi_information, d)时,rpi_information成员是一个文件句柄,而不是copyfile所期望的文件类型之一的文件路径。传递文件名:

for d in usbpathlist:
    copyfile('rpi_information.txt', d)

相关问题 更多 >