繁体   English   中英

如何将目录或制作目录添加到当前路径并使用文件

[英]How to add directory or make directory to current path and use file

我想在当前路径上创建一个目录并在该路径中添加一个 excel 文件并在脚本中使用该 excel 文件....请帮助我目前正在做

my_excel_file = Path(sys.argv[2])
if not my_excel_file.is_file():
    print ("Excel File not exist")
    logging.error("Excel File not exist")
    exit(-2)

但我想在当前路径中添加目录'/tmp/old excel/n.xlsx'并使用 n.xlsx 文件

如果不存在,此代码将创建一个目录和文件。 您还可以写入该文件:

import os

filename = "tmp/old excel/n.xlsx"
if not os.path.exists(os.path.dirname(filename)):
       os.makedirs(os.path.dirname(filename))

with open(filename, "w") as f:
   f.write("content")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM