如何正确传递路径变量?

时间:2019-09-05 10:01:25

标签: python python-3.x easygui

这将查找文件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传递给变量?

哦,随时批评逻辑并简化

1 个答案:

答案 0 :(得分:1)

  

我得到错误行15 ..._ isdir:路径应为字符串,字节或os.PathLike,而不是列表

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

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

真正的代码是:

if not os.path.isdir(indir):

在这种情况下,错误的确是可以预料的,因为在上一行中:

indir = loadsavefile.readlines()

file.readlines()确实确实返回了列表,如所记录。您可能希望改用file.readline().strip()(如果您确定所需信息当然在第一行)。

  

TypeError:chdir:路径应为字符串,字节或os.PathLike,而不是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")
    不知道这里的“ r”是什么

Err ...听起来可能像是在开玩笑,但it's documented too;-)

  

哦,随时批评逻辑并简化

好吧,您有一个明显的重复项(对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"))