Python游走一/

2024-09-30 14:15:22 发布

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

我正在尝试找到一些文件,创建一个文件夹,并将这些文件移到那里。在

def test():
    try:
        logfile = "C:\\Users\\alkis\\Desktop\\testouter\\test"
        result_dir = os.path.join(logfile, "testzip")
        print result_dir
        os.makedirs(result_dir)
        os.chmod(result_dir, stat.S_IWRITE)
        kpath = logfile + "\\*.jpg"
        print kpath
        files = glob.glob(kpath)
        for file in files:
           filename = os.path.splitext(file)[0]
           print filename
           os.chmod(filename, stat.S_IWRITE)
           shutil.move(filename, result_dir)
    except Exception, e:
        #shutil.rmtree(result_dir)
        print e.__doc__ + "\r\n"
        print e.message
    return

我得到的错误是:MS-Windows OS call failed 我检查我的文件的权限,它们不是只读的。在


Tags: 文件pathtestosdirfilesresultfilename
1条回答
网友
1楼 · 发布于 2024-09-30 14:15:22

列出每个文件,删除扩展名,然后尝试移动该文件名。在

扩展名是文件名的一部分,不要删除它。Windows Exlorer只在显示文件时隐藏扩展名。在

您也不需要对文件名调用os.chmod();只需跳过该步骤:

for file in files:
    filename = os.path.splitext(file)[0]
    print filename
    shutil.move(filename, result_dir)

相关问题 更多 >

    热门问题