Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to get the absolute path of a file in python

Shulou Source: shulou.com Published: 2022-05-31 11:12:23 09月23日 Update

Today, the editor will share with you the relevant knowledge points about how to obtain the absolute path of files in python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Specific operation methods:

1. Find the python program in the start menu of win system.

2. Find and open the idle tool program in the python program directory.

3. Click "File" in the upper left corner of the idle tool and select "New File" to create a new shell script.

4. Enter the following instructions in the shell script to import the os module.

Import os # Import os module

5. Finally, you can obtain the absolute path of the file by the following methods.

Print (os.path.abspath (file path)) # output absolute path

Related operation instructions:

Os.getcwd () # gets the current working directory

Os.path.getsize (file name) # View the size of the specified file

Os.listdir () # returns all file and directory names under the specified directory

Related examples:

#! / usr/bin/python

#-*-coding: UTF-8-*-

Import os

File='/root/runoob.txt' # file path

Print (os.path.abspath (file)) # output absolute path

These are all the contents of the article "how to get the absolute path to a file in python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Files paths directories knowledge articles programs contents tools instructions methods modules scripts output different very large size most examples that is the upper left corner Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Docker Microsoft NVidia