Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python脚本为每个计划任务创建新的shapefile名称_Python - Fatal编程技术网

如何使用Python脚本为每个计划任务创建新的shapefile名称

如何使用Python脚本为每个计划任务创建新的shapefile名称,python,Python,我刚刚创建了一个简单的python脚本,它通过一个包含多段线shapefile的文件夹并合并它们。通过Windows 8任务计划程序,我已计划在需要时运行脚本 我现在想做的就是修改我的脚本,这样我就可以稍微更改每个shapefile输出的名称。例如,第1周的脚本名称为MergedTracks_1,第2周的脚本名称为MergedTracks_2,第3周的脚本名称为MergedTracks_3,等等 有没有人知道如何用我现在的剧本做到这一点?我正在运行ArcGIS 10.2。如果可能的话,我将非常感

我刚刚创建了一个简单的python脚本,它通过一个包含多段线shapefile的文件夹并合并它们。通过Windows 8任务计划程序,我已计划在需要时运行脚本

我现在想做的就是修改我的脚本,这样我就可以稍微更改每个shapefile输出的名称。例如,第1周的脚本名称为MergedTracks_1,第2周的脚本名称为MergedTracks_2,第3周的脚本名称为MergedTracks_3,等等

有没有人知道如何用我现在的剧本做到这一点?我正在运行ArcGIS 10.2。如果可能的话,我将非常感谢您的帮助。下面是我目前在PythonWin中使用的脚本。提前谢谢你

import arcpy, os  

outPut = r"C:\Users\student2\Desktop\WeedTracksMergeScript\Output" # Output  
arcpy.env.workspace = r"C:\Users\student2\Desktop\WeedTracksMergeScript" 
shplist =  arcpy.ListFeatureClasses('*.shp')
print shplist # prints polyline .shp list in current arcpy.env.workspace
arcpy.Merge_management(shplist, os.path.join(outPut, "MergedTracks_1.shp"))  
print "Done"

您可以使用pickle跟踪上次写入的文件名,然后将其作为逻辑的一部分来确定下一个文件名格式

请在此处查看教程:

主要想法是这样做:

下面是一些伪代码:

load pickle file

if pickle file exists
    load pickle file
    use logic to increment new filename
else:
    use default file name

do logic for your work and write with set file name

write to pickle file for the new file name
下面是一个关于如何使用pickle的快速示例:

import pickle
from os.path import exists

pickle_file = "current_file_name.pickle"

if exists(pickle_file):
    with open(pickle_file, "rb") as pkr:
        current_filename = pickle.load(pkr)
else:
    current_filename = "current_file_1"

with open(pickle_file, "wb") as pkw:
    pickle.dump(current_filename, pkw)
我认为邮戳(字符串)不会那么复杂,而且可以说更有用

许多格式选项记录在


您需要在“out_path”之后立即移除闭合括号。
import datetime
dd = datetime.datetime.now().strftime("%Y%m%d")
shpfile = os.path.join(out_path), "MergedTracks_{}.shp".format(dd))
arcpy.Merge_management(shplist, shpfile)