1

试图让这行代码工作,无论我如何更改格式,我都会遇到问题:

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'
4

4 回答 4

4

您在这里缺少“*”:

>>> os.path.join('foo', *['a','b'])
'foo/a/b'

您必须在此处使用星号运算符才能将列表项作为解压缩的变量参数列表传递给方法。

于 2012-12-18T06:01:10.147 回答
1

@user1833746 首先得到了答案,所以如果您接受其中任何一个,请接受那个:)

除了解包之外,如果您不打算使用结果列表,您可以将['s更改(为使其成为真正的生成器(而不是创建一个列表然后迭代它)。操作符“*解包”,这意味着项目的各个组件将被传递给函数。正如您在下面的代码中看到的,os.path.join接受“两个”参数:(a路径名)和*p(任意数量的路径组件)。如您所见,您可以使用此语法提供任意数量的附加路径名参数(即没有固定path_component1path_component2变量)。在您的情况下,一旦您获得了值的生成器,您就可以将它们“解包”为单个值(而不是单个值listgenerator对象),这os.path.join函数然后处理:

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
于 2012-12-18T06:23:55.863 回答
0

在前面加上 *[str(x) for x in [year, month, day]]

*[str(x) for x in [year, month, day]]

于 2012-12-18T06:01:32.450 回答
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):
于 2012-12-18T06:04:04.020 回答