Итак, у меня есть скрипт на Python, который мне дал мой друг, но у меня нет опыта работы с Python.Вот код для него:
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
Для строки, которая читает newDirectory = path.join(thisDir + "\Extracted Items", file + " - Extracted Items")
Я хочу изменить это для доступа к родительскому каталогу thisDir
, а затем создать \Extracted Items
папка.Кто-нибудь знает, как лучше всего получить доступ к родительскому каталогу в python?