我需要将打印功能输出发送到文件,但我收到错误请帮助。在执行过程中无法将打印功能输出发送到文件和出现错误
我的脚本:
#get time for to create log file
timestamp = time.strftime("%Y%m%d-%H%M%S")
## Open the file with read only permit
f = open ('file1', 'r')
## Read the first line
line = f.readline()
## If the file is not empty keep reading line one at a time
## till the file is empty
while line:
print timestamp
ip,owner = line.split()
print ip
logfile = ("log-" + timestamp)
print('################################### ' + ip + ' logs') >>logfile
print logfile
FNULL = open(logfile, 'a')
sshconnection = subprocess.call(["ssh", ip, "uptime"], stdout=FNULL, stderr=FNULL, shell=False)
if sshconnection == 0:
print('Connection Established to Remote Host ' + ip)
else:
print('Please check the Remote Host Reachable or Password less configured' + ip)
print owner
newpassword()
后执行代码我得到以下错误:
print('################################### ' + ip + ' logs ####################################') >> logfile
TypeError: unsupported operand type(s) for >>: 'str' and 'str'
我使用python2.7
你为什么使用'>>'?这不像C,在Python中是整数上使用的移位运算符。 – TidB