The tempfile module

The tempfile module

The tempfile module

This module allows you to quickly come up with unique names to use for temporary files.

Example: Using the tempfile module to create filenames for temporary files
# File: tempfile-example-1.py

import tempfile
import os

tempfile = tempfile.mktemp()

print "tempfile", "=>", tempfile

file = open(tempfile, "w+b")
file.write("*" * 1000)
file.seek(0)
print len(file.read()), "bytes"
file.close()

try:
    # must remove file when done
    os.remove(tempfile)
except OSError:
    pass

tempfile => C:\TEMP\~160-1
1000 bytes

The TemporaryFile function picks a suitable name, and opens the file. It also makes sure that the file is removed when it’s closed (under Unix, you can remove an open file and have it disappear when the file is closed. On other platforms, this is done via a special wrapper class).

Example: Using the tempfile module to open temporary files
# File: tempfile-example-2.py

import tempfile

file = tempfile.TemporaryFile()

for i in range(100):
    file.write("*" * 100)

file.close() # removes the file!
posted on 2013-08-29 11:03  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/p/3288641.html

Import arcpy module import arcpy # Get input feature class and output feature class input_fc = arcpy.GetParameterAsText(0) output_fc = arcpy.GetParameterAsText(1) # Get field list of input feature class fields = arcpy.ListFields(input_fc) # Create a list of field names to keep field_names = ["OID@", "SHAPE@", "SHAPE_LENGTH", "SHAPE_AREA"] # Loop through input feature class fields and add field names to field_names list for field in fields: if not (field.name in field_names): # If the field name is not in the field_names list field_names.append(field.name) # Add the field name to the field_names list # Use Make Feature Layer to create a layer from the input feature class arcpy.MakeFeatureLayer_management(input_fc, "temp_layer") # Use Add Field Delimiters to get the correct syntax for field names delimited_fields = arcpy.AddFieldDelimiters("temp_layer", field_names[0]) for i in range(1, len(field_names)): delimited_fields += "," + arcpy.AddFieldDelimiters("temp_layer", field_names[i]) # Use Copy Features to create the output feature class with only the specified fields arcpy.CopyFeatures_management("temp_layer", output_fc, delimited_fields) # Delete the temporary layer arcpy.Delete_management("temp_layer")运行错误:Traceback (most recent call last): File "D:\多对一空间连接\删除所有字段.py", line 28, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 2429, in CopyFeatures raise e ExecuteError: 执行失败。参数无效。 ERROR 000800: 该值不是 | DEFAULTS | TEXT_UTF16 | MAX_FILE_SIZE_4GB | MAX_FILE_SIZE_256TB | GEOMETRY_OUTOFLINE | BLOB_OUTOFLINE | GEOMETRY_AND_BLOB_OUTOFLINE | TERRAIN_DEFAULTS | MOSAICDATASET_DEFAULTS | MOSAICDATASET_INLINE 的成员。 执行(CopyFeatures)失败。 执行(删除所有字段)失败。请改正代码
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值