Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of os.path.expanduser in python

Shulou Source: shulou.com Published: 2022-06-02 09:24:33 10月22日 Update

What is the role of os.path.expanduser in python, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

On linux, my account is kwsy, and the user's home directory is / home/kwsy. The following code shows how to use the expanduser function.

Python 3.7.0 (default, Jun 28 2018, 13:15:42)

[GCC 7.2.0]:: Anaconda, Inc. On linux

Type "help", "copyright", "credits" or "license" for more information.

> import os

> os.path.expanduser ('~ / .config /')

'/ home/kwsy/.config/'

In the directory ~ / .config/, ~ this is the representative of the user's home directory. If your account is sheng, then in shell, ~ / .config/ is equivalent to / home/sheng/.config,shell will help you find the absolute address of the directory, but in the python program, this equivalence relation is not true. Executing os.mkdir ('~ / test') will report an error, and python cannot recognize ~ / test as / home/sheng/test. You can use the expanduser function to replace ~ with the current user's hone working directory.

Under windows, my account name is zhangdongsheng and my working directory is C:\ Users\ zhangdongsheng

Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

> import os

> os.path.expanduser ('~ / .config /')

C:\\ Users\\ zhangdongsheng/.config/' will it be helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Directory user function that is equivalence account work help function clarity code representative content address this situation article novice more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Microsoft vpn Shulou Information