从一个文件写入另一个文件

时间:2019-08-16 08:46:22

标签: python file-handling

  1. secret_msg和路径作为参数
    1. 以a +模式打开路径中提到的文件
    2. 在上述打开的文件中写入secret_msg的内容。我该怎么做呢 ?它说错误
  

'str'对象没有属性'write'    4.关闭文件

返回: 该函数没有返回参数

message_parts=[secret_msg_3, secret_msg_1, secret_msg_4, secret_msg_2]
final_path= user_data_dir + '/secret_message.txt'

#Code starts here
secret_msg = " ".join(message_parts)
def write_file(secret_msg, path) :
    open("path" , 'a+' )
    path.write(secret_msg)
    path.close()

write_file(secret_msg,final_path)

print(secret_msg)

1 个答案:

答案 0 :(得分:0)

您需要open文件,然后调用[]方法。

这里是一种方式:

write

或使用def write_file(secret_msg, path): f = open(path, 'a+') f.write(secret_msg) f.close()

with

我建议您看看How to write a file with Python

希望有帮助!