如何正确传递路径变量?

2024-04-25 13:24:15 发布

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

这将查找文件indirsavefile.txt,如果找到,则读取存储在indir变量中的单个目录路径。如果该目录存在,它将尝试进入该目录。你知道吗

如果找不到indirsavefile.txt,或者找不到写入其中的路径,它将打开一个目录选择器对话框,其结果将尝试另存为新的indir变量。你知道吗

import os

from os import path
indirsavefile = path.expandvars(r'%APPDATA%\lootbot\indirsavefile.txt')
print(indirsavefile)
print("proceeding to conditional...")

if os.path.isfile(indirsavefile):
    print("indirsavefile.txt found")
    # not sure what "r" is here for
    with open(indirsavefile, "r") as loadsavefile:
        indir = loadsavefile.readlines()
        print("marker1")
        if not os.path.isdir(indir):
            print("marker2")
            import easygui
            indir = print(easygui.diropenbox("Locate your DOWNLOADS directory root"))
else:
    print("indirsavefile.txt not found")
    import easygui
    indir = print(easygui.diropenbox("Locate your DOWNLOADS directory root"))
    # here save the new indir location to indirsavefile.txt

print("resulting indir location is:")
print(indir)
print("proceed to enter directory...")

os.chdir(indir)
print("Current working directory is",os.getcwd())

有一些无关的print语句可以帮助我追踪bug,对此表示抱歉。找到保存文件后,我得到这个

Traceback (most recent call last):
  File "D:/SYSTEM/CODING/PYTHON/import.py", line 14, in <module>
    if not os.path.isdir(indir):
TypeError: _isdir: path should be string, bytes or os.PathLike, not list
C:\Users\Administrator\AppData\Roaming\lootbot\indirsavefile.txt
proceeding to conditional...
indirsavefile.txt found
marker1

Process finished with exit code 1

如果找不到文件

C:\Users\Administrator\AppData\Roaming\lootbot\indirsavefile.txt
proceeding to conditional...
indirsavefile.txt not found
E:\IMPORT
Traceback (most recent call last):
resulting indir location is:
  File "D:/SYSTEM/CODING/PYTHON/import.py", line 28, in <module>
None
    os.chdir(indir)
proceed to enter directory...
TypeError: chdir: path should be string, bytes or os.PathLike, not NoneType

Process finished with exit code 1

如何将目录变量传递给os.path.isdiros.chdir,并从easygui.diropenbox传递给变量?你知道吗

哦,你可以随意批评一下逻辑,简化一下


Tags: topathimport目录txtisosnot
1条回答
网友
1楼 · 发布于 2024-04-25 13:24:15

I get the error line 15..._isdir: path should be string, bytes or os.PathLike, not list

此错误与您的代码不匹配。我想不是

if not os.path.isdir(r"indir"):

真正的代码是:

if not os.path.isdir(indir):

在这种情况下,错误确实是意料之中的,因为在前一行中:

indir = loadsavefile.readlines()

file.readlines()确实返回了一个列表,如文档所示。你可能想要file.readline().strip()(当然,如果你确定你想要的信息在第一行的话)。你知道吗

TypeError: chdir: path should be string, bytes or os.PathLike, not NoneType

这也是意料之中的:

indir = print(easygui.diropenbox("Locate your DOWNLOADS directory root"))

print()确实返回None,这也是有文献记载的。你想要:

indir = easygui.diropenbox("Locate your DOWNLOADS directory root")
print(indir)

open(indirsavefile, "r")
not sure what "r" is here for

呃。。。这听起来像是一个笑话,但是it's documented too;-)

Oh, and feel free to critique the logic and simplify

好吧,你有一个明显的重复(调用easyguy)。你知道吗

首先:您希望将import语句移到脚本的顶部(并不是说它在技术上改变了任何东西,而是it's the convention,它确实有助于wrt/maintability)

然后要避免重复代码:

import easygui

# ...

indir = None # means: we haven't a usable value yet

if os.path.isfile(indirsavefile):
    with open(indirsavefile, "r") as loadsavefile:
        indir = loadsavefile.readline().strip()
        if not os.path.isdir(indir):
            # ok, still not a usable value
            indir = None

if indir is None:
    indir = easygui.diropenbox("Locate your DOWNLOADS directory root"))

相关问题 更多 >