• Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
No Result
View All Result
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
No Result
View All Result
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
No Result
View All Result
Home Tutorials

Navigate go up one directory in Python using paths.

by Sick Codes
April 24, 2020 - Updated on June 24, 2020
in Tutorials
0
import os
from os.path import basename
path = "."
path = os.path.realpath(path)
path = path[:-len(basename(path))]

If you want to know what this does:
path starts as

.

then os.path.realpath(arg) turns it into

/abd/def/ghik

which is the current working directory.
Now path is a string: “/abd/def/ghik”

basename(path)

will give us “ghik” which is a 4 letter word.
The directory above this directory is the realpath -4 letters, from the end.
We can use [from:to] to select a range of items in a python list.
So we use from 0 to -4

path[:-len(basename(path))]

If you want to remove the last “/”, use the range 0:-5

path = path[:-(len(basename(path))+1)]
Next Post

Add Swap RAM on CentOS 8 (one line, automatically)

amg video convert ffmpeg linux

Linux/Mac: Ffmpeg render/convert or export video using AMD GPU Graphics Card in Arch Linux, Mint, Debian, Manjaro

Install Fail2Ban on CentOS 8, 9. Works on RHEL 8 and RHEL 9 too.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training

© 2017-2021 Sick.Codes

@sickcodes

@sickcodes

@sickcodes

Discord Server

sickcodes.slack.com

t.me/sickcodeschat

./contact_form