os.path.exists()始终返回false

用户9611200

我正在尝试检查文件是否在指定目录中退出。如果是这样,那么我将文件移到另一个目录。这是我的代码

def move(pnin, pno):
    if (os.path.exists(pnin)):
        shutil.move(pnin, pno)

这是pnin和pno的例子

pnin='D:\\extracted\\extrimg_2016000055202500\\2016000055202500_65500000007006_11_6.png'
pno=D:\folder\discarded

我有8000多个输入目录。我从.n的输出复制了此pnin print(pnin)。如示例中那样在外部定义pnin时,if语句起作用。但是当我想迭代地运行“移动”功能时,if语句永远不会执行。可能是什么问题,我该如何解决?这是我如何调用移动功能:

def clean_Data(inputDir, outDir):
    if (len(listf) > 1):
        for l in range(1,len(listf)):
            fname = hashmd5[m][l]    
            pathnamein = os.path.join(inputDir, fname)
            pathnamein = "%r"%pathnamein
            pathnameout = outfile
            move(pathnamein, pathnameout)

当我尝试下面的代码时,它不会给出任何输出。为循环工作。当我print(pathnamein)在for循环中使用它时,它将显示pathnamein的所有值。

def move(pnin, pno):
    os.path.exists(pnin)
吹牛

您应该使用反斜杠来转义pno字符串中的反斜杠

pno='D:\\folder\\discarded'

或使用原始字符串代替:

pno=r'D:\folder\discarded'

否则\f将被视为换页符。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

python os.path.exists 返回 false

从文本文件获取“字符串(路径)”时,File.Exists(Path)始终返回false

当file_path是相对路径时,os.path.isfile(file_path)返回false,为什么?

os.path.split似乎返回错误

python 函数 os.path.exists() 当我给一个 bool 值时返回 True

os.path.exists返回错误的w /包含空格的转义路径

os.path.exists与os.path.isdir之间的优缺点

os.path.normpath() 在 ubuntu 中比较 python 中的两个路径时返回 False

os.path.isdir()在无法访问但现有目录上返回false

os.path.isdir()向现有文件夹返回false

os.path.isfile()对于linux smb网络驱动器上的文件返回false

os.path.isfile()对于网络驱动器上的文件返回false

os.path.isdir()和os.path.isfile()都在脚本中返回False,但在Python控制台中返回True(就像应该那样)

os.path AttributeError:'str'对象没有属性'exists'

尝试通过变量使用 os.path.exists 但出现错误

os.path.exists()将创建该文件

python os.path.isfile()对于标量返回true

python os.path.isfile()对于某些整数返回True

os.path.dirname(__ file__)返回空

os.path.dirname(__ file__)返回空

Python os.path.isdir为点返回true

python中的os.path.join返回“错误”路径?

File.Exists()始终返回false

Windows 10上的Python:os.rename()找不到os.path.exists()找到的文件吗?

为什么“ os.path.join”和“ os.path.dirname(__ file__)”返回不同的分隔符?

错误出现在这一行 os.path.exists()

python os.path.exists()对于存在的nfs挂载目录文件失败

os.path.exists对Windows上名为“ CON.csv”的文件提供误报

当文件夹名称中有空格时使用`os.path.exists`?