所以我有一个朋友给我的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中访问父目录的最佳方式是什么吗?在