为什么这个 python 代码没有给出具体的结果?
Why is this python Code not giving a specifc result?
我是 python 初学者。
为了练习,我正在编写 python 代码,将数据从 test1
传输到 test2
,然后清除 test1
,以及从 [=] 传输数据13=] 到 test3
.
但是当我 运行 我得到的这段代码 test3
是空的。
from sys import argv # this argument will import argument variables
from os.path import exists
files_12 = "{} {}"
print (files_12.format(1, 2))
script, test1, test2, test3 = argv # here i get arguments
target1 = open(test1) # this opens test1 and variable set at target1
new_target = target1.read() # this reads target1 which is originally test1
print (f"Number of strings in target1 is {len(new_target)}") # in this i have given print to count number of strings in test1, why new target because i have assigned variable of new_target in read
print (f"Does the out file exist? {exists(test2)}") # this will check if if exist or not
target2 = open(test2, "w") # this will open test2
target2.write(new_target)# this will write data of test1 in test2
target3 = open(test2) # this will pen test2
veerynew = target3.read()# this will read test2
target_new = open(test1, "w") #this will open test1 in write mode which is put by "r"
target_new.truncate()# this will clead data of test1
target4 = open(test3, "w")# this will open test3
target4.write(veerynew)#this will write data of test2 in test2
target1.close()# this will close file 1
target2.close()#this will close file 2
target3.close()#this will close file 3
target4.close()#this will close file 4
在这些行中:
target2 = open(test2, "w") # this will open test2
target2.write(new_target)# this will write data of test1 in test2
target3 = open(test2) # this will pen test2
veerynew = target3.read()# this will read test2
您的代码打开 test2
进行写入,写入,然后打开它进行读取而不先关闭它。如果你在两者之间关闭它:
target2 = open(test2, "w") # this will open test2
target2.write(new_target)# this will write data of test1 in test2
target2.close()
target3 = open(test2) # this will pen test2
veerynew = target3.read()# this will read test2
你会得到你期望的行为。
另外,这一行
print (f"Number of strings in target1 is {len(new_target)}")
清楚地表明您需要来自 file.read()
的字符串列表,但是如果您查看输出,您会看到您得到的是字节数,而不是字符串数。如果您想获取字符串列表,请改为调用 file.readlines()
。
我是 python 初学者。
为了练习,我正在编写 python 代码,将数据从 test1
传输到 test2
,然后清除 test1
,以及从 [=] 传输数据13=] 到 test3
.
但是当我 运行 我得到的这段代码 test3
是空的。
from sys import argv # this argument will import argument variables
from os.path import exists
files_12 = "{} {}"
print (files_12.format(1, 2))
script, test1, test2, test3 = argv # here i get arguments
target1 = open(test1) # this opens test1 and variable set at target1
new_target = target1.read() # this reads target1 which is originally test1
print (f"Number of strings in target1 is {len(new_target)}") # in this i have given print to count number of strings in test1, why new target because i have assigned variable of new_target in read
print (f"Does the out file exist? {exists(test2)}") # this will check if if exist or not
target2 = open(test2, "w") # this will open test2
target2.write(new_target)# this will write data of test1 in test2
target3 = open(test2) # this will pen test2
veerynew = target3.read()# this will read test2
target_new = open(test1, "w") #this will open test1 in write mode which is put by "r"
target_new.truncate()# this will clead data of test1
target4 = open(test3, "w")# this will open test3
target4.write(veerynew)#this will write data of test2 in test2
target1.close()# this will close file 1
target2.close()#this will close file 2
target3.close()#this will close file 3
target4.close()#this will close file 4
在这些行中:
target2 = open(test2, "w") # this will open test2
target2.write(new_target)# this will write data of test1 in test2
target3 = open(test2) # this will pen test2
veerynew = target3.read()# this will read test2
您的代码打开 test2
进行写入,写入,然后打开它进行读取而不先关闭它。如果你在两者之间关闭它:
target2 = open(test2, "w") # this will open test2
target2.write(new_target)# this will write data of test1 in test2
target2.close()
target3 = open(test2) # this will pen test2
veerynew = target3.read()# this will read test2
你会得到你期望的行为。
另外,这一行
print (f"Number of strings in target1 is {len(new_target)}")
清楚地表明您需要来自 file.read()
的字符串列表,但是如果您查看输出,您会看到您得到的是字节数,而不是字符串数。如果您想获取字符串列表,请改为调用 file.readlines()
。