我只知道如何编写用于GIS的python。这个代码还有更多使用arcpy和地理处理工具....这只是我坚持的开始部分,因为我试图准备数据,所以我可以使用zipped文件夹中的shapefile来完成剩下的工作。我的剧本
我试图提示用户输入要搜索的目录。要使用此脚本,它将搜索压缩的zip文件,然后将所有文件解压缩到同一目录。
import zipfile, os
# ask what directory to search in
mypath = input("Enter .zip folder path: ")
extension = ".zip"
os.chdir(mypath) # change directory from working dir to dir with files
for item in os.listdir(mypath):
if item.endswith(extension):
filename = os.path.abspath(item)
zip_ref = zipfile.ZipFile(filename)
zip_ref.extractall(mypath)
zip_ref.close()
尝试了所有建议,但仍然存在以下问题:
import zipfile, os
mypath = input("Enter folder: ")
if os.path.isdir(mypath):
for dirpath, dirname, filenames in os.listdir(mypath):
for file in filenames:
if file.endswith(".zip"):
print(os.path.abspath(file))
with zipfile.ZipFile(os.path.abspath(file)) as z:
z.extractall(mypath)
else:
print("Directory does not exist.")
答案 0 :(得分:0)
如果位置无效,您的第一个if-block将否定else-block。我会完全删除'else'运算符。如果你保留它,if-check有效地杀死程序。 “if folderExist”足以替换else。
import arcpy, zipfile, os
# ask what directory to search in
folder = input("Where is the directory? ")
# set workspace as variable so can change location
arcpy.env.workspace = folder
# check if invalid entry - if bad, ask to input different location
if len(folder) == 0:
print("Invalid location.")
new_folder = input("Try another directory?")
new_folder = folder
# does the above replace old location and re set as directory location?
# check to see if folder exists
folderExist = arcpy.Exists(folder)
if folderExist:
# loop through files in directory
for item in folder:
# check for .zip extension
if item.endswith(".zip"):
file_name = os.path.abspath(item) # get full path of files
print(file_name)
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(folder) # extract all to directory
zip_ref.close() # close file
如果您没有使用原始代码,这可能会更整洁:
import zipfile, os
from tkinter import filedialog as fd
# ask what directory to search in
folder = fd.askdirectory(title="Where is the directory?")
# loop through files in directory
for item in os.listdir(folder):
# check for .zip extension
if zipfile.is_zipfile(item):
file_name = os.path.abspath(item) # get full path of files
# could string combine to ensure path
# file_name = folder + "/" + item
print(file_name)
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(folder) # extract all to directory
zip_ref.close() # close file
答案 1 :(得分:0)
我不确定使用arcpy
。然而...
要迭代目录中的条目,请使用os.listdir
:
for entry_name in os.listdir(directory_path):
# ...
在循环内,entry_name
将是directory_path
目录中项目的名称。
当检查它是否以“.zip”结尾时,请记住比较是区分大小写的。使用str.lower
时,您可以使用str.endswith
有效地忽略大小写:
if entry_name.lower().endswith('.zip'):
# ...
要获取条目的完整路径(在本例中为.zip),请使用os.path.join
:
entry_path = os.path.join(directory_path, entry_name)
将此完整路径传递给zipfile.ZipFile
。
答案 2 :(得分:0)
[增订]
我能够用以下代码解决相同问题
import os
import zipfile
mypath = raw_input('Enter Folder: ')
if os.path.isdir(mypath):
for file in os.listdir(mypath):
if file.endswith('.zip'):
with zipfile.ZipFile(os.path.join(mypath, file)) as z:
z.extractall(mypath)
else:
print('Directory does not exist')