python - Python中存在文件吗?

标签 python file-io path exists

Possible Duplicate:
Pythonic way to check if a file exists?

如何使用 python 2.6 检查文件是否存在?

如果文件存在,则运行 exec redo.py。 如果文件不存在则执行文件start.py

文件大小为0kb,但名称为Xxx100926.csv

答案似乎是

 from os path import exists
 from __future__ import with_statement

    if exists('Xxx100926.csv'):
      from redo import main
      #execfile(u'C:\redo.py')
    else:
      from start import main
      #execfile(u'C:\start.py') 
      with open(Xxx100926.csv, 'a'): pass

 #and run main function
 main()

最佳答案

你可以将 main 函数放在 redo.pystart.py 中,然后

from os path import exists

if exists('Xxx100926.csv'):
  from redo import main
else:
  from start import main

#and run main function
main()

关于python - Python中存在文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3805132/

相关文章:

python - Docker:优雅地停止 django 服务器

python - 记录文件打开调试的位置 "ResourceWarning: unclosed file"

c++ - 将文本文件读入包含空格的数组

python - "OSError: [Errno 22] Invalid argument"读取一个大文件时

java - 保存文件的相对路径

python - 格式化 pandas 表对两列进行排序

python - 为什么 sorted() 函数在传递 3 个参数时说它只需要一个参数?

.net - 为什么 IO.Directory.CreateDirectory 不应该成功?

java - JUnit 不运行

node.js - 无法使用 Git Bash 运行 mongo/mongod,但它在 Git CMD 中运行(mongo : not found)