Python 3 Simple Examples get current folder and go to parent

Опубликовано: 06 Октябрь 2024
на канале: Softhints - Python, Linux, Pandas
2,305
13

Python change current directory(up to parent) 3 Examples

https://blog.softhints.com/python-cha...

os.chdir("..")
pathlib - p.parent
os.chdir(os.path.dirname(os.getcwd()))

Example 1

import os
print(os.path.abspath(os.curdir))
os.chdir("..")
print(os.path.abspath(os.curdir))

Example 2

from pathlib import Path
p = Path("/home/user/myfile.txt")
print(p.parent)
print(p.parent.parent)


Example 3

import os
print(os.path.dirname(os.getcwd()))
os.chdir(os.path.dirname(os.getcwd()))
print(os.path.dirname(os.getcwd()))

---------------------------------------------------------------------------------------------------------------------------------------------------------------
Code store

https://bitbucket.org/softhints/

Socials

Facebook:   / 435421910242028  
Facebook:   / softhints  
Twitter:   / softwarehints  
Discord:   / discord  


If you really find this channel useful and enjoy the content, you're welcome to support me and this channel with a small donation via PayPal.

PayPal donation https://www.paypal.me/fantasyan