如何在Python中访问父目录
我有一个朋友给我的Python脚本,但我没有Python方面的经验。这是它的代码:如何在Python中访问父目录,python,path,directory,parent,traversal,Python,Path,Directory,Parent,Traversal,我有一个朋友给我的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) IMAG
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+“\extracteditems”,file+“-extracteditems”)
我想修改它以访问
thisDir
的父目录,然后创建\extracteditems
文件夹。有人知道python中访问父目录的最佳方法是什么吗?您可以使用os.path
模块中的split
函数访问父目录
from os.path import dirname, split, isdir
parent_dir = lambda x: split(x)[0] if isdir(x) else split(dirname(x))[0]
由于您没有Python方面的经验,请简要介绍一下代码:lambda
语句定义了一个内联函数。在该函数中,三元条件首先计算给定路径x
是否为目录。如果适用,则使用split
功能分割路径。如果路径x
不是目录,则首先计算路径的目录名,然后拆分路径。拆分路径如下所示:
C:\Foo\Bar\file.spam=>(C:\Foo\Bar\,file.spam)
现在查看调用函数时发生的情况:
path = r"C:\Foo\Bar\file.spam"
print "Parent directory of " + path + ":", parent_dir(path)
C:\Foo\Bar\file.spam的父目录:C:\Foo\
注意:在我看来,文件的父目录就是文件目录的父目录。如果您不这样定义它,那么您的函数也可能如下所示:
from os.path import dirname, split, isdir
parent_dir = lambda x: split(x)[0] if isdir(x) else dirname(x)
因此,从最初发布的代码中,我不必导入任何内容,因为我已经从操作系统导入路径中导入了
,对吗?当代码应用于我时,它是否会像parent\u dir=lambda thisDir:split(thisDir)[0]如果isdir(thisDir)else split(dirname(thisDir))[0]
那么newDirectory=path.join(parent\u dir+“\Extracted Items”,file+“-Extracted Items”)
?啊,那么我应该使用parent\u dir=lambda x:split(x)[)[](x)
因为我对父目录的定义是我当前所在文件夹的直接父目录?是,如果只使用dirname(x)
您将收到文件x
所在的文件夹。是的,路径已经包含在内。如果不直接导入函数,您可以使用点符号访问它们。parent\u dir=lambda x:path.split(x)[0]If path.isdir(x)else path.dirname(x)
。在上次截取的代码中,您需要调用该函数来激活它,并为其指定要处理的路径:newDirectory=path.join(parent_dir(currentDirectory),“提取的项目”,file+“-提取的项目”)
。请注意,path.join
会在传递给它的参数之间自动添加\
,因此您可以将path.join(thisDir+“\Extracted Items”,file,…)
替换为path.join(thisDir,“Extracted Items”,file,…)
import os.path,sys
CURRENT_DIR = os.path.dirname(__file__).replace('\\','/')
PARENT_DIR = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir))