80 lines
2.3 KiB
Python
Executable File
80 lines
2.3 KiB
Python
Executable File
import os
|
||
|
||
|
||
def getFiles():
|
||
folders = [] # 子目录
|
||
file_tree = {} # 所有文件
|
||
# 获得Data文件夹的绝对路径
|
||
data_path = os.path.abspath('./Data')
|
||
for root, dirs, files in os.walk(data_path):
|
||
if dirs != []:
|
||
folders = dirs
|
||
# 将子目录换成绝对路径
|
||
for i in range(len(folders)):
|
||
folders[i] = data_path + '/' + folders[i]
|
||
# 得到所有文件的路径
|
||
for path in folders:
|
||
file = []
|
||
for root, dirs, files in os.walk(path):
|
||
file.append(files)
|
||
file_tree[path] = file
|
||
|
||
return file_tree
|
||
|
||
|
||
def process(files):
|
||
csv_folder = "" # 转换后存储路径
|
||
if not os.path.exists('./csv'):
|
||
os.makedirs('./csv')
|
||
csv_folder = os.path.abspath('./csv')
|
||
try:
|
||
command = "" # 命令行命令
|
||
for folder in files.keys():
|
||
# 确认csv目录存在,避免系统错误
|
||
csv_path = csv_folder + '/' + folder.split('Data/')[1].split('/')[0]
|
||
if not os.path.exists(csv_path):
|
||
os.makedirs(csv_path)
|
||
|
||
for file in files[folder][0]:
|
||
# 组合 jar 命令
|
||
command = 'java -jar GISDataReader.jar ' + folder + '/' + file + ' ' + csv_folder + '/' + \
|
||
folder.split('Data/')[1].split('/')[0] + '/' + file.replace('.dat', '.csv')
|
||
# 执行命令
|
||
os.system(command)
|
||
print(command)
|
||
except FileNotFoundError:
|
||
print("文件未找到")
|
||
|
||
|
||
def default():
|
||
files = getFiles()
|
||
process(files)
|
||
|
||
|
||
def singleFile():
|
||
origin = input("请粘贴dat文件的绝对路径:")
|
||
dest = input("请输入csv文件目标路径,留空则默认为本路径:")
|
||
if dest == "":
|
||
dest = "./"
|
||
command = 'java -jar GISDataReader.jar ' + origin + ' ' + dest + '/' + origin.split('/')[-1].replace('.dat', '.csv')
|
||
try:
|
||
os.system(command)
|
||
except FileNotFoundError:
|
||
print("文件未找到!")
|
||
exit(1)
|
||
print("已完成转换任务!拜拜┏(^0^)┛")
|
||
|
||
|
||
def chooseMode():
|
||
print("1. 默认路径转换模式")
|
||
print("2. 单文件自选模式")
|
||
choice = int(input("请选择脚本运行模式:"))
|
||
if choice == 1:
|
||
default()
|
||
elif choice == 2:
|
||
singleFile()
|
||
|
||
|
||
if __name__ == '__main__':
|
||
chooseMode()
|
||
singleFile() |