【问题标题】:Why does this not work? (os.makedirs)为什么这不起作用? (os.makedirs)
【发布时间】:2016-07-06 07:15:20
【问题描述】:

请帮我解决下面的代码并说明为什么os.makedirs 不起作用? (请不要注意缩进:它们在原版中是正确的,只是我正在掌握这个网站上的 HTML 编码。)

import os,pprint,sys
while True:
    print()
    oq=input('Press the first directory:   ')
    print()
    print()
    ow=input('Press the next directory/name:   ')
    print()
    p2=input('Continue with next directory? yes or no: ').lower()
    if p2=='no':
        break
    print()
    oe=input('Press the next directory/name:   ')
    print()
    p3=input('Continue with next directory? yes or no: ').lower()
    if p3=='no':
        break
    print()
    oee=input('Press the next directory/name:   ')
    print()
    p4=input('Continue with next directory? yes or no: ').lower()
    if p4=='no':
        break
    print()
    ot=input('Press the next directory/name:   ')
    print()
    p5=input('Continue with next directory? yes or no: ').lower()
    if p5=='no':
        break
    print()
    oy=input('Press the next directory/name:   ')
    print()
    p6=input('Continue with next directory? yes or no: ').lower()
    if p6=='no':
        break
    print()
    ou=input('Press the next directory/name:   ')
    print()
    p7=input('Continue with next directory? yes or no: ').lower()
    if p7=='no':
        break
    print()
    if p2=='no':
        os.makedirs(oq+'\\'+ow)
    if p3=='no':
        os.makedirs(oq+'\\'+ow+'\\'+oe)
    if p4=='no':
        os.makedirs(oq+'\\'+ow+'\\'+oe+'\\'+oee))
    if p5=='no':
        os.makedirs(oq+'\\'+ow+'\\'+oe+'\\'+oee+'\\'+ot)
    if p6=='no':
        os.makedirs(oq+'\\'+ow+'\\'+oe+'\\'+oee+'\\'+ot+'\\'+oy)
    if p7=='no':
        os.makedirs(oq+'\\'+ow+'\\'+oe+'\\'+oee+'\\'+ot+'\\'+oy+'\\'+ou) 
    ppp=input('Wannna continue???')
    if ppp=='no':
        sys.exit()

【问题讨论】:

  • 您需要将代码格式化为更易于阅读。像这样将它粘贴成一大堆可能会使通常会回答您问题的人直接跳过它。我建议查看帖子的代码格式帮助部分,然后编辑/重新提交。
  • 我希望你不介意,...但我已经处理好了你的缩进。不过,我确实保持每条线与其他线相同。要执行我刚刚所做的,突出显示代码并按 Ctrl-k。
  • “不起作用”在什么情况下?您是否在代码中放置了任何调试消息以查看它实际上进入了那里?还使用os.path.join() 连接路径,和/或os.sep 作为分隔符,而不是硬编码`\\`

标签: python


【解决方案1】:

您的代码甚至不会执行,因为您的代码中有语法错误:

if p3=='no':
    os.makedirs(oq+'\\'+ow+'\\'+oe)
if p4=='no':
    os.makedirs(oq+'\\'+ow+'\\'+oe+'\\'+oee)) # <-- here
if p5=='no':
    os.makedirs(oq+'\\'+ow+'\\'+oe+'\\'+oee+'\\'+ot)

确实:

$ python machdirs2.py 
  File "machdirs2.py", line 48
    os.makedirs(oq+'\\'+ow+'\\'+oe+'\\'+oee))
                                        ^
SyntaxError: invalid syntax
$ 

当您修复该语法错误时,我们看到您使用了input,这需要您引用您的"input"。我这样做并输入了两个引用的目录名称,然后输入了"no" 只是为了点击break 退出while 而不处理makedirs if-spaghetti。我还没有深入挖掘...

实现目标的更好方法是重新开始,重新思考,然后从头开始实施,避免代码中的所有繁琐重复和陷阱:

import os

path = ""
while True:
    nxt = raw_input("next level or empty to quit: ")
    if not nxt:
        break
    path = os.path.join(path, nxt)

print path
os.makedirs(path)

导致:

$ python machdirs.py 
next level or empty to quit: a
next level or empty to quit: b
next level or empty to quit: c
next level or empty to quit: 
a/b/c
$ find . -type d -print
.
./a
./a/b
./a/b/c
$ 

在您的 Windows 机器上尝试代码,您会发现它可以跨平台运行,而您的代码在我的 Linux 机器上无法运行。

【讨论】:

  • 好吧,伙计们,我通过将整个东西分成两部分得到了解决方案;第一部分采用目录和文件名的名称,第二部分创建文件夹。我将它们放在 while True 命令下,并将全局 p2 例如,如果 p2=='no': global p2 break 等等。这对您有帮助并感谢大家的回答,我也会尝试一下。
猜你喜欢
  • 2012-11-17
  • 2023-03-16
  • 2011-10-13
  • 2011-11-21
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多