Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6245719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:33:22+00:00 2026-05-24T12:33:22+00:00

So I have a Python script that was given to me by a friend

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:33:24+00:00Added an answer on May 24, 2026 at 12:33 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script that needs to execute an external program, but for
I have a little python script that pulls emails from a POP mail address
I have a python script start.py that executes well from the command line. There
I have a python script that analyzes a set of error messages and checks
I have a python script that is a http-server: http://paste2.org/p/89701 , when benchmarking it
I have a python script that I would like to add a Shutdown when
I have a python script that has to launch a shell command for every
I have a Python script that calls an executable program with various arguments (in
How do I have a Python script that can accept user input and how
I have a simple Python script that I want to stop executing if a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.