简体   繁体   中英

How to add directory or make directory to current path and use file

I want to make a directory on current path and add an excel file in that path and use that excel file in script....please help currently I am doing

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)

but i want to add directory '/tmp/old excel/n.xlsx' in current path and use n.xlsx file

This code will create a directory and file if does not exist. You can also write to that file :

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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