Get Documents directory with python automatically

0

I need to automatically get the document folder address of any Windows with Python.

Currently in my script I determined it as shown in the variable main_folder , however whenever I change from PC I have to change that address. Below is also one of the uses of this variable in my script .

Creating directories and sub directories. If anyone knows any other method, I will be very grateful.

import arcpy
main_folder = r"C:\Users\Edeson Bizerril\Documents\myEBpy_Files"

# EXEMPLO DE USO
# Determinando diretório
input_folder = main_folder + "\Input\"
input_folder_SRTM = self.input_folder + r"SRTMs\"

create_folder(input_folder)

create_folder(input_folder_SRTM)

# Método para criação de diretórios
def create_folder(path):
    if not os.path.isdir(path):
        os.makedirs(path)

Unfortunately I need to determine the reference folder in my work, but I would like Python to automatically identify and return the exact same result in the main_folder variable.

    
asked by anonymous 09.10.2018 / 23:23

1 answer

3

In the standard python library there is the os.path module which has several methods to assist in manipulating operating system directories.

To get the user's home directory you can use the expanduser function.

from os import path


main_folder = path.join(path.expanduser("~"), "Documents/myEBpy_Files")

On Windows it will return: C:\Users\Usuario\Documents\myEBpy_Files

    
10.10.2018 / 14:51