Python &引用;类型错误:';非类型';“对象不可编辑”;应该列在什么清单上
我有一些代码是根据我的发现编写的,但是由于我的名声不好,我不能发表评论来提问。我还浏览了所有其他与这个错误相关的帖子,但是,可能是由于我对编程和Python不熟悉,我还没有找到解决方案 好的,我有以下代码:Python &引用;类型错误:';非类型';“对象不可编辑”;应该列在什么清单上,python,list,function,Python,List,Function,我有一些代码是根据我的发现编写的,但是由于我的名声不好,我不能发表评论来提问。我还浏览了所有其他与这个错误相关的帖子,但是,可能是由于我对编程和Python不熟悉,我还没有找到解决方案 好的,我有以下代码: import zipfile, os def dirEntries(dir_name): ''' Creates a list of all files in the folder 'dir_name' which we assign when we call the functi
import zipfile, os
def dirEntries(dir_name):
''' Creates a list of all files in the folder 'dir_name' which we assign when we call the function later'''
fileList = []
'''creates an empty list'''
for file in os.listdir(dir_name):
'''for all files in the directory given'''
dirfile = os.path.join(dir_name, file)
'''creates a full file name including path for each file in the directory'''
if os.path.isfile(dirfile) and os.path.splitext(dirfile)[1][1:]!='lock':
'''if the full file name above is a file and it does not end in 'lock' it will be added to the list created above'''
fileList.append(dirfile)
def makeArchive(fileList, archive, root):
"""
'fileList' will be a list of file names - full path each name
'archive' will be the file name for the archive with a full path (ex. "C:\\GIS_Data\\folder.zip")
"""
a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
for f in fileList:
a.write(f, os.path.relpath(f, root))
'''I don't completely understand this, but the 'relpath' part seemed integral to not having the entire folder structure saved in the zip file'''
a.close()
makeArchive(dirEntries(ptdir), ptdir+"pts.zip", ptdir)
makeArchive(dirEntries(polydir), polydir+"polys.zip", polydir)
“ptdir”和“polydir”在代码的前面部分中定义
我得到以下信息:
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "C:\GIS_Data\Working\Python\DatabaseExport\ExportScriptTEST.py", line 181, in <module>
makeArchive(dirEntries(ptdir), ptdir+"pts.zip", ptdir)
File "C:\GIS_Data\Working\Python\DatabaseExport\ExportScriptTEST.py", line 177, in makeArchive
for f in fileList:
TypeError: 'NoneType' object is not iterable
回溯(最近一次呼叫最后一次):
RunScript中的文件“C:\Python27\ArcGIS10.1\Lib\site packages\pythonwin\pywin\framework\scriptutils.py”,第326行
主目录中的exec codeObject__
文件“C:\GIS\u Data\Working\Python\DatabaseExport\ExportScriptTEST.py”,第181行,在
makeArchive(dirEntries(ptdir),ptdir+“pts.zip”,ptdir)
文件“C:\GIS\U Data\Working\Python\DatabaseExport\ExportScriptTEST.py”,第177行,在makeArchive中
对于文件列表中的f:
TypeError:“非类型”对象不可编辑
我已经在交互窗口中完成了这个过程,当我分解函数并一点一点地输入它们时,填充列表没有问题,但是当我一起运行这个过程时,我得到了错误。是由于某种原因没有填充列表的问题,还是发生了其他问题
任何帮助都将不胜感激 此函数中没有返回任何内容
dirEntries(dir\u name)
,因此默认情况下它返回None
在末尾添加return语句以解决此问题:
def dirEntries(dir_name):
''' Creates a list of all files in the folder 'dir_name' which we assign when we call the function later'''
fileList = []
'''creates an empty list'''
for file in os.listdir(dir_name):
'''for all files in the directory given'''
dirfile = os.path.join(dir_name, file)
'''creates a full file name including path for each file in the directory'''
if os.path.isfile(dirfile) and os.path.splitext(dirfile)[1][1:]!='lock':
'''if the full file name above is a file and it does not end in 'lock' it will be added to the list created above'''
fileList.append(dirfile)
return fileList
此函数中没有返回任何内容
dirEntries(dir\u name)
,因此默认情况下它返回None
在末尾添加return语句以解决此问题:
def dirEntries(dir_name):
''' Creates a list of all files in the folder 'dir_name' which we assign when we call the function later'''
fileList = []
'''creates an empty list'''
for file in os.listdir(dir_name):
'''for all files in the directory given'''
dirfile = os.path.join(dir_name, file)
'''creates a full file name including path for each file in the directory'''
if os.path.isfile(dirfile) and os.path.splitext(dirfile)[1][1:]!='lock':
'''if the full file name above is a file and it does not end in 'lock' it will be added to the list created above'''
fileList.append(dirfile)
return fileList
这里有一个关于“低声誉”的提示…只需注册另一个堆栈溢出站点。您的代表将获得+100的提升,然后可以随意发表评论。请注意,在python中使用docstrings
“
作为评论不是惯用做法。您应该使用实际的评论#
。docstring解释如何使用函数,注释用于解释函数的工作方式。这里是对“低声誉”的提示“事情……只需注册另一个堆栈溢出站点。您的代表将获得+100的提升,然后可以随意发表评论。请注意,在python中使用docstrings“
作为评论并不惯用。您应该使用实际的评论#
。docstring解释如何使用函数,而注释用于解释函数的工作原理。