How to use win environment variable pathlib to save files?

2024/9/26 3:24:18

I'm trying to use win environment variable like %userprofile%\desktop with pathlib to safe files in different users PC.

But I'm not able to make it work, it keep saving in on the running script dir.

import pathlib
from datetime import datetime

a = r'%userprofile%\desktop\test2' b = 'test' def path(path_name, f_name):date = datetime.now().strftime("%d%m-%H%M%S")file_name = f'{f_name}--{date}.xlsx'file_path = pathlib.Path(path_name).joinpath(file_name)file_dir = pathlib.Path(path_name)try:file_dir.mkdir(parents=True, exist_ok=True)except OSError as err:print(f"Can't create {file_dir}: {err}")return file_pathpath(a, b)
Answer

pathlib does have Path.home(), which expands to the user's home directory.

from pathlib import Path
print(Path.home())    # On Windows, it outputs: "C:\Users\<username>"# Build a path to Desktop
desktop = Path.home() / "Desktop"
print(desktop)    # On Windows, it outputs: "C:\Users\<username>\Desktop"
https://en.xdnf.cn/q/71504.html

Related Q&A

Difference between starting firestore emulator through `firebase` and `gcloud`?

What is the difference between starting the firestore emulator through: firebase emulators:start --only firestoreand: gcloud beta emulators firestore startBoth options allow my python app to achieve co…

PyInstaller icon option doesnt work on Mac

I ran the following command on my mac and created an .app file.pyinstaller --icon icon.icns --noconsole -n testApp main.pyHowever, the generated .app file does not show the icon.icon.icns is specified …

Error Installing scikit-learn

When trying to install scikit-learn, I get the following error:Exception:Traceback (most recent call last):File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2…

Issues downloading Graphlab dependencies get_dependencies()

I am having trouble when I try to download the dependencies needed to run graphlab. I do import graphlab I get the following:ACTION REQUIRED: Dependencies libstdc++-6.dll and libgcc_s_seh-1.dll not fou…

Django Tastypie slow POST response

Im trying to implement a Tastypie Resource that allows GET & POST operations following a per user-permission policy, the model is pretty simple (similar to the Note model in Tastypie documentation)…

Extract a region of a PDF page by coordinates

I am looking for a tool to extract a given rectangular region (by coordinates) of a 1-page PDF file and produce a 1-page PDF file with the specified region:# in.pdf is a 1-page pdf file extract file.pd…

Is it possible to concatenate QuerySets?

After a search of a database I end up with an array of querysets. I wanted to concatenate these queryset somewhat like we can do with list elements. Is this possible or maybe there an altogether better…

Pickling a Python Extension type defined as a C struct having PyObject* members

I am running C++ code via Python and would like to pickle an extension type.So I have a C++ struct (py_db_manager) containing pointers to a database object and a object manager object (both written in …

Generating random ID from list - jinja

I am trying to generate a random ID from a list of contacts (in Python, with jinja2) to display in an HTML template. So I have a list of contacts, and for the moment I display all of them in a few cell…

Unit testing Flask app running under uwsgi

I’m relatively new to python and am looking for a pythonic way to handle this practice. I’ve inherited a fairly trivial Python 2.7 Flask app that runs under uwsgi that I want to add some unit tests t…