Jan-28-2021, 10:06 AM
(This post was last modified: Jan-28-2021, 10:06 AM by DeaD_EyE.
Edit Reason: bb-code does not work in code block
)
Use the modern API from pathlib.
Your path can be separated into different components:
Your path can be separated into different components:
- Original: C:\Users\blake\PycharmProjects\newb\out
- Path: C:\Users\blake\PycharmProjects\newb\
File: out
- Home: C:\Users\blake\
Pycharm: PycharmProjects
Project: newb
File: out
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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!