python中的文件父路径怎么表达_如何在Python中访问父目录

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值