尝试让这行代码正常工作,无论我如何改变格式,我都会遇到问题:
if not os.path.exists(os.path.join(IncludeSettings.FILE_URL, [str(x) for x in [year, month, day]])):
(年,月,日)可以是整数或字符串。
回溯:
Traceback (most recent call last):
File "/home/tinyup/dev/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.3-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 117, in toolbar_tween
response = _handler(request)
File "/home/tinyup/dev/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.3-py2.7.egg/pyramid_debugtoolbar/panels/performance.py", line 55, in resource_timer_handler
result = handler(request)
File "/home/tinyup/dev/lib/python2.7/site-packages/pyramid-1.4a3-py2.7.egg/pyramid/tweens.py", line 20, in excview_tween
response = handler(request)
File "/home/tinyup/dev/lib/python2.7/site-packages/pyramid-1.4a3-py2.7.egg/pyramid/router.py", line 161, in handle_request
response = view_callable(context, request)
File "/home/tinyup/dev/lib/python2.7/site-packages/pyramid-1.4a3-py2.7.egg/pyramid/config/views.py", line 342, in rendered_view
result = view(context, request)
File "/home/tinyup/dev/lib/python2.7/site-packages/pyramid-1.4a3-py2.7.egg/pyramid/config/views.py", line 456, in _class_requestonly_view
response = getattr(inst, attr)()
File "/home/tinyup/dev/tinyuploads/tinyuploads/views/share.py", line 56, in view_process
return self.handle_file_upload(self.request.params['file'], shareID)
File "/home/tinyup/dev/tinyuploads/tinyuploads/views/share.py", line 101, in handle_file_upload
self.save(file, newFileName, isImage, uploadTime)
File "/home/tinyup/dev/tinyuploads/tinyuploads/views/share.py", line 166, in save
if not os.path.exists(os.path.join(IncludeSettings.FILE_URL, [str(x) for x in [year, month, day]])):
File "/home/tinyup/dev/lib/python2.7/posixpath.py", line 66, in join
if b.startswith('/'):
AttributeError: 'list' object has no attribute 'startswith'
答案 0 :(得分:4)
你在这里错过了'*':
>>> os.path.join('foo', *['a','b'])
'foo/a/b'
您必须在此处使用星号运算符才能将列表项作为解压缩变量参数列表传递给方法。
答案 1 :(得分:1)
@ user1833746首先得到答案,所以如果您接受其中任何一个,请接受以下答案:)
除了解包之外,如果您不打算使用结果列表,您可以将[
更改为(
以使其成为真正的生成器(而不是创建列表,然后迭代通过)。 *
运算符'解包',意味着项的各个组件将传递给函数。正如您在下面的代码中看到的,os.path.join
接受'两个'参数:a
(路径名)和*p
(任意数量的路径组件)。如您所见,您可以使用此语法提供任意数量的其他路径名参数(即,没有固定的path_component1
,path_component2
变量。在您的情况下,一旦获得了值生成器,就可以将它们“解压缩”为单个值(而不是单个list
或generator
对象),然后os.path.join
函数处理:< / p>
In [1]: import os
In [2]: os.path.join('/home/myname', *(str(x) for x in ('one', 'two', 'three')))
Out[2]: '/home/myname/one/two/three'
In [3]: os.path.join??
Type: function
Base Class: <type 'function'>
String Form: <function join at 0x7f4944c31a28>
Namespace: Interactive
File: /usr/lib/python2.6/posixpath.py
Definition: os.path.join(a, *p)
Source:
def join(a, *p):
"""Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded."""
path = a
for b in p:
if b.startswith('/'):
path = b
elif path == '' or path.endswith('/'):
path += b
else:
path += '/' + b
return path
答案 2 :(得分:0)
在[str(x) for x in [year, month, day]]
之前添加*
*[str(x) for x in [year, month, day]]
答案 3 :(得分:0)
if not os.path.exists(os.path.join(IncludeSettings.FILE_URL, str(year), str(month), str(day))):
为了便于阅读:
fname = os.path.join(IncludeSettings.FILE_URL, str(year), str(month), str(day))
if not os.path.exists(fname):