Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Access Parent Directory in Python

So I have a Python script that was given to me by a friend of mine, but I have no experience in Python. This is the code for it:

from os import path, chdir, listdir, mkdir, getcwd
from sys import argv
from zipfile import ZipFile
from time import sleep

#Defines what extensions to look for within the file (you can add more to this)
IMAGE_FILE_EXTENSIONS = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.tif', '.tiff')

#Changes to the directory in which this script is contained
thisDir,_ = path.split(path.abspath(argv[0]))
chdir(thisDir)

#Lists all the files/folders in the directory
fileList = listdir('.')
for file in fileList:

    #Checks if the item is a file (opposed to being a folder)
    if path.isfile(file):

        #Fetches the files extension and checks if it is .docx
        _,fileExt = path.splitext(file)
        if fileExt == '.docx':

            #Creates directory for the images
            newDirectory = path.join(thisDir + "\Extracted Items", file + " - Extracted Items")
            if not path.exists(newDirectory):
                mkdir(newDirectory)

            currentFile = open(file, "r")
            for line in currentFile:
                print line

            sleep(5)



            #Opens the file as if it is a zipfile
            #Then lists the contents
            try:
                zipFileHandle = ZipFile(file)
                nameList = zipFileHandle.namelist()

                for archivedFile in nameList:
                    #Checks if the file extension is in the list defined above
                    #And if it is, it extracts the file
                    _,archiveExt = path.splitext(archivedFile)
                    if archiveExt in IMAGE_FILE_EXTENSIONS:
                        zipFileHandle.extract(archivedFile, newDirectory)
                    if path.basename(archivedFile) == "document.xml":
                        zipFileHandle.extract(archivedFile, newDirectory)
                    if path.basename(archivedFile) == "document.xml.rels":
                        zipFileHandle.extract(archivedFile, newDirectory)
            except:
                pass

For the line that reads newDirectory = path.join(thisDir + "\Extracted Items", file + " - Extracted Items")

I want to modify that to access the parent directory of thisDir and then create the \Extracted Items folder. Does anyone know what the best way to access the parent directory is in python?

like image 932
This 0ne Pr0grammer Avatar asked Mar 16 '26 21:03

This 0ne Pr0grammer


2 Answers

import os.path,sys
CURRENT_DIR = os.path.dirname(__file__).replace('\\','/')
PARENT_DIR = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir))
like image 167
Serjik Avatar answered Mar 19 '26 11:03

Serjik


You can access the parent directory using the split function from the os.path module.

from os.path import dirname, split, isdir
parent_dir = lambda x: split(x)[0] if isdir(x) else split(dirname(x))[0]

As you do not have experience in Python, a short explanation of the code:
The lambda statement defines an inline -function. Within this function, the ternary condition first evaluates if the given path x is a directory. If it applies, the path is splitted using the split function. If the path x is not a directory, first the directory name of the path is calculated and then the path is splitted.
Splitting a path looks like this: C:\Foo\Bar\file.spam => (C:\Foo\Bar\, file.spam)

Now see what's happening when calling the function:

path = r"C:\Foo\Bar\file.spam"
print "Parent directory of " + path + ":", parent_dir(path)

Parent directory of C:\Foo\Bar\file.spam: C:\Foo\

Note: In my opinion the parent directory of a file is the parent-directory of the directory of the file. If you don't define it like this, your function could also look like this:

from os.path import dirname, split, isdir
parent_dir = lambda x: split(x)[0] if isdir(x) else dirname(x)
like image 41
Niklas R Avatar answered Mar 19 '26 10:03

Niklas R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!