Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';str';对象没有属性';搜索';_Python_Python 3.x - Fatal编程技术网

Python 属性错误:';str';对象没有属性';搜索';

Python 属性错误:';str';对象没有属性';搜索';,python,python-3.x,Python,Python 3.x,我是Python3的noob。我已经广泛地寻找了问题的解决方案,但我不明白为什么.seek()方法不起作用。 我想做一个程序,将一个档案的信息复制到另一个档案的最后一个。我知道我可以使用追加或其他方法,但事实上,让我感兴趣的是,这就是为什么在这个程序中会发生attributeerror 提前谢谢 from sys import argv script, from_file, to_file=argv print(f"Are you sure that you want to copy {from

我是Python3的noob。我已经广泛地寻找了问题的解决方案,但我不明白为什么.seek()方法不起作用。 我想做一个程序,将一个档案的信息复制到另一个档案的最后一个。我知道我可以使用追加或其他方法,但事实上,让我感兴趣的是,这就是为什么在这个程序中会发生attributeerror

提前谢谢

from sys import argv
script, from_file, to_file=argv
print(f"Are you sure that you want to copy {from_file} to {to_file}?")
print("Ready hit RETURN to continue, CTRL-C to abort")
input()
origin_file=""
final_file=""

try:
  file1=open(from_file,'r')
  file2=open(to_file,'r+')
  file1=file1.read()

except:
  print('There is not a file')

file2.seek(0,2)
file2.write(file1)

print(final_file)

已解决:我无法将seek与字符串对象一起使用。:)

final\u file=file2.read()
final\u file=file2.read()
final\u file=file2.read()如果试图对字符串调用seek,则使用
final\u file=file2.read()读取文件。也许您想使用
file2.seek()
?如果您想在文件末尾添加数据,请以附加模式打开它:
'a'
。然后只需写,无需搜索。如果您试图在字符串上调用seek,则可以使用
final\u file=file2.read()读取文件。也许您想使用
file2.seek()
?如果您想在文件末尾添加数据,请以附加模式打开它:
'a'
。那就写吧,不用找了。
final_file=file2.read() <---- return string...