gpt4 book ai didi

python - 为什么我的 Python 脚本编写的是 Windows 风格的回车符?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:42 25 4
gpt4 key购买 nike

我正在尝试编写一个脚本来创建一个 fabfile,保存它然后运行它。到目前为止,这是我的代码:

#!/usr/bin/python

bakery_internalip = "10.10.15.203"

print "[....] Preparing commands to run within fabfile.py"

fabfile = open("sfab.py", "w")
fabfile.write("from fabric.api import run, sudo, task\n\n@task\ndef myinstall():\n\tsudo('yum install httpd')")
fabfile.close

print "Running Fab Commands"

import subprocess
subprocess.call(['fab', '-f', 'sfab.py', '-u ec2-user', '-i', 'id_rsa', '-H', bakery_internalip, 'myinstall'])

我的fabfile的内容如下:

[root@ip-10-10-20-82 bakery]# cat sfab.py
from fabric.api import run, sudo, task

@task
def myinstall():
sudo('yum install httpd')

我的脚本在运行时出现以下错误:

Fatal error: Fabfile didn't contain any commands!

但是,如果我在该文件上运行 dos2unix,然后运行以下命令,它工作正常:

 fab -f sfab.py -H localhost myinstall

最佳答案

简单的错字 fabfile.close 应该是 fabfile.close()

不关闭就运行会给你:

Running Fab Commands

Fatal error: Fabfile didn't contain any commands!

Aborting

with open("sfab.py", "w") as fabfile:
fabfile.write("from fabric.api import run, sudo, task\n\n@task\ndef myinstall():\n\tsudo('yum install httpd')")

总是像上面那样使用 with 打开你的文件,它会自动为你关闭它们并避免这些简单的错误。

关于python - 为什么我的 Python 脚本编写的是 Windows 风格的回车符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24002137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com