我在这里有一个简单的bash命令,用于我正在用Python编写的脚本,我已经做了很多搜索,但没有找到一个简单的答案。。Here is just a little snippet (there are a lot of lines like this):

echo "    ServerName  www.${hostName}" >> $prjFile

现在我知道它看起来像:
print ("ServerName www.", hostName) >> prjFile

正确的?但那不管用。。

最佳答案

您可以尝试一个简单的:

myFile = open('/tmp/result.file', 'w') # or 'a' to add text instead of truncate
myFile.write('whatever')
myFile.close()

就你而言:
myFile = open(prjFile, 'a') # 'a' because you want to add to the existing file
myFile.write('ServerName www.{hostname}'.format(hostname=hostname))
myFile.close()

关于python - Python:回传到文件(如Bash),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16857834/

10-14 19:08