Saving to a specific directory - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Saving to a specific directory (/thread-32207.html) |
Saving to a specific directory - blakefindlay - Jan-28-2021 Just wondering what the best way is to open and save the file to a directory I have called 'out' C:\Users\blake\PycharmProjects\newb\out renter = open("renter_" + str(flname.get()) + "_" + str(year) + "-" + str(month) + "-" + str(day), "w" ) renter.write("Employee: " + employee_dropdown.get()) renter.write("\n") RE: Saving to a specific directory - DeaD_EyE - Jan-28-2021 Use the modern API from pathlib. Your path can be separated into different components:
from pathlib import Path HOME = Path.home() # <-- this is dynamic. It depends on OS and logged-in User PROJECTS = "PycharmProjects" PROJECT = "newb" FILE = "out" # path objects support concatenation with / complete_path_to_file = HOME / PROJECTS / PROJECT / FILE # path objects to have the open() method, which replicates the built-in open() with complete_path_to_file.open("w") as fd: fd.write("Hello World") print("Written data to", complete_path_to_file) # but works also with the built-in open() with open(complete_path_to_file, "w") as fd: fd.write("Hello World") print("Written data to", complete_path_to_file) RE: Saving to a specific directory - Ron_Crafter - Jan-28-2021 how post in python forum |