簡體   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