如何正确传递路径变量?

How to pass path variables correctly?

这会查找文件 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 语句可以帮助我追踪错误,对此深表歉意。找到保存文件后,我得到这个

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 传递给变量?

哦,随时批评逻辑并简化

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() 确实 return 一个列表,如文档所述。您可能需要 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() 确实 return 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/可维护性)

那你要避免重复代码:

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"))