H
H
hohopro352021-08-28 16:28:55
Python
hohopro35, 2021-08-28 16:28:55

How to create a variable for a folder on a computer?

How to make a variable that will always type in the same path on any computer? Let's say a variable that on any computer will indicate the path to the AppData/Roaming folder.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kokapuk, 2021-08-28
@kokapuk

path = f'C:\\Users\\{getpass.getuser()}\\AppData\\Roaming'

V
Vindicar, 2021-08-28
@Vindicar

Well, "on any computer" is a strong word. Still, the computer may not be windows.
But you have two options.
First, there is the %APPDATA% environment variable, which stores the path to AppData/Roaming for the current user. See os.environ .
Secondly, there is the win32com module, which allows you to use COM modules for Windows, incl. provided by the system. One of them deals with the paths to standard folders.

from win32com.shell import shell, shellcon
#путь к "мои документы". можно задавать разные константы CSIDL_XXX
doc_folder = shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, None, 0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question