【问题标题】:Dirrectory name invalid目录名称无效
【发布时间】:2013-07-17 14:38:46
【问题描述】:

下面的代码给了我很长一段时间以来我见过的最奇怪的错误。当我不使用它时,它会参考/*.*? 代码:

t_src = src.get()
t_dst = dst.get()

print t_src

try:
    for item in os.listdir(t_src):
        print item

        s = os.path.abspath(os.path.join(t_src,item))
        d = os.path.abspath(os.path.join(t_dst, item))

        for filename in os.listdir(s):
            try:
                if os.path.isfile(filename):
                    print filename


                elif os.path.isdir(filename):
                    print "Possible Dirrectory"
                    print filename
                    #shutil.copytree(filename, d)
            except:
                print "Unexpected error:", traceback.format_exc()

except:
    print "Unexpected error:", traceback.format_exc()

错误:

C:\Windows\system32\cmd.exe /c python CopyDir.py
AS.txt
Unexpected error: Traceback (most recent call last):
  File "CopyDir.py", line 46, in copyFiles
    for filename in os.listdir(s):
WindowsError: [Error 267] The directory name is invalid: 'C:\\Users\\JG
\\Desktop\\Hello World\\AS.txt/*.*'

src.get() 来自 Tkinter,因为我使用的是 GUI。这没有问题。 t_src 产生以下示例

C:/Users/JG/Desktop/Hello World

主要目标是输出文件夹中的所有文件,然后查找两者之间的更改,您必须在其中更改相同的文件等。但是正如您所看到的,它生成的文件结构不是正确。

我正在使用 Windows Python 2.7.5 和文本编辑器 VIM(非常棒)。

【问题讨论】:

    标签: windows python-2.7 directory


    【解决方案1】:

    ITEM 是一个文件名 - 将文件名提供给 os.listDir 将导致我得到的错误。要批准该问题,请使用'os.walk(t_src)'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 2014-03-07
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      相关资源
      最近更新 更多