【问题标题】:How to put the txt file in the Downloads directory (python, odoo)如何将txt文件放到Downloads目录下(python、odoo)
【发布时间】:2017-09-04 05:35:25
【问题描述】:
    a = random.randint(1, 9999)
    name = 'filename' + str(a * 7) + '.txt'
    print '>>', a
    ROOT = os.path.abspath(os.sep)
    filepath = os.path.join(ROOT, name)
    os.chdir(filepath)
    file = open(filepath, 'w')

它显示一个 OSError。 OSError: [Errno 2] 没有这样的文件或目录:'/Users/Downloads'

【问题讨论】:

  • 你能显示整个脚本的输出吗?

标签: python file save openerp


【解决方案1】:

您正在尝试 os.chdir 到一个文件。您的变量name 是以.txt 结尾的文件名,您要附加到ROOT。只需在将文件名添加到 filepath 变量之前更改目录即可。

【讨论】:

    【解决方案2】:

    请删除 os.chdir(filepath) 行,无需更改目录。使用下面的代码。

    a = random.randint(1, 9999)
    name = 'filename' + str(a * 7) + '.txt'
    print '>>', a
    ROOT = os.path.abspath(os.sep)
    filepath = os.path.join(ROOT, name)
    file = open(filepath, 'w')
    

    【讨论】:

    • 谢谢.. 它有效,没有错误 OSError 已经但它说,[Errno 13] Permission denied: '/filename6839.txt' 我应该如何处理权限?
    • 文件保存在根目录 /.因此,没有权限将其保存到该用户,这就是该错误出现的原因,要解决此问题,您需要设置用户对该位置的读/写权限。
    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多