Python directory operation summary

--Import

import os

--Get the current working directory --This prints the directory you are currently in, not the location of the file

os.getcwd()

print(os.getcwd())
# usr/var/www/lib

--Get the location of the running file

print(__file__)
# usr/var/www/lib/hoge.py

--Get the directory of running files --Use os.path.dirname () --No last slash

print(os.path.dirname(__file__))
# usr/var/www/lib

--Get the file name of the running file (after the last slash) --Use os.path.basename () --No slash in the head

print(os.path.basename(__file__))
# hoge.py

--The above method displays the relative path from the current directory --If you want to display with an absolute path --Use os.path.abspath () --This is also valid in a production environment

print(os.path.abspath(__file__))
# usr/var/www/lib

--If you want to move to the directory that contains the currently running file --Use os.chdir ()

os.chdir(os.path.dirname(os.path.abspath(__file__)))
# 

Recommended Posts

Python directory operation summary
Python Summary
Python summary
[python] vector operation
Python tutorial summary
Python OS operation
[Python] Matrix operation
python related summary
Python basics summary
Python + Selenium Frequently used operation method summary
Summary about Python scraping
[Python] File / directory operations
Python Django tutorial summary
Correspondence summary of array operation of ruby and python
Summary about Python3 + OpenCV3
Python function argument summary
Python AI framework summary
Python iteration related summary
Python logical operation stumbling
Summary of Python arguments
Python decorator operation memo
[python] Array slice operation
Python3 programming functions personal summary
Summary of Python3 list operations
What's new in Python 3.10 (Summary)
S3 operation with python boto3
Standard input / summary / python, ruby
Python web programming article summary
python pandas study recent summary
Python Competitive Programming Site Summary
Python data type summary memo
Face detection summary in Python
Create a directory with python
What's new in Python 3.9 (Summary)
Get home directory with python
Python Crawling & Scraping Chapter 4 Summary
Summary of Hash (Dictionary) operation support for Ruby and Python
Virtual Environment Version Control Summary Python
[Python] Check the current directory, move the directory
[Python] Operation memo of pandas DataFrame
Summary if using AWS Lambda (Python)
Python
R / Python coding rule link summary
A brief summary of Python collections
Machine learning summary by Python beginners
[python] week1-3: Number type and operation
selenium case study summary python pyCharm
Create python directory Support if directory exists
Python package management tool personal summary
Python parallel / parallel processing sample code summary
[Linux] File and directory operation commands
Basic Python operation 2nd: Function (argument)
[Introduction to Udemy Python 3 + Application] Summary
Python import directory order (on anaconda)
[Automation with python! ] Part 2: File operation
[Python] File operation using if statement
Summary of Python indexes and slices
Command for the current directory Python
[OpenCV; Python] Summary of findcontours function
Python basic operation 1st: List comprehension notation
Summary of various for statements in Python