覆盖文件的一部分会导致空字符串 Python 3
Overwriting part of a file results in empty string Python 3
编辑:解决方案只是改变我打开文件的方式(感谢 Primusa),而不是我替换信息的方式。
我试图覆盖文件的一部分,但我的代码不起作用。当它 运行 时,它会完全删除文件中的所有内容,不留下任何东西。
这是我的代码:
for fname in store:
with open(fname + ".txt", "w+") as f:
for line in f:
c = store[fname]
x = (c-1)%len(line.split(","))
y = m.floor((c-1)/len(line.split(",")))
for n in range(y):
f.readline()
ldata = line.strip("\n").split(",")
for i in range(len(ldata)):
ldata[i] = int(ldata[i])
if w == None:
ldata[x] += 1
elif w == True:
ldata[x] += 2
elif w == False:
ldata[x] -= 1
#s = line.find("\n") - 1
#f.truncate(s)
f.write(str(ldata) + "\n")
密钥:
fname
:文件名对应的字符串变量,不带文件类型。
store
:包含文件名键作为字符串和整数值的字典。
f
: 包含多行整数列表的文件。
c
:整型变量,用于确定要访问的整数。
x
和y
:值设置为文件中要访问的整数的列和行(分别)的变量。
w
:存储为布尔值或None
的变量,用于确定访问的整数是否应该增加或减少,以及增加多少。
s
: 当前未使用的整数变量,用于 运行 分类文件的一部分。
使用示例:
假设我有一个文件 Foo.txt
,其中存储了以下信息:
0,2,3,7
11,3,6,4
我想将“6”值增加 2,所以我 运行 将 w
设置为 True
的代码并将 "Foo" : 7
添加到 store
,因为“6”是文件中的第 7 个数字(无论列表长度如何)。
应该发生什么:
Foo.txt
已修改,现在包含:
0,2,3,7
11,3,8,4
实际发生了什么:
Foo.txt
仍然存在,但现在包含:
也就是说它是空的。
我的代码有什么问题?我是否错误地处理了文件、变量计算、语法或其他完全不同的东西?
with open(fname + ".txt", "w+") as f:
以 "w+" 模式打开文件会截断文件,这意味着它会删除其中的所有内容。在这条语句之后你所做的所有操作都是在一个空文件上。
我建议以读取模式打开文件:
with open(fname + ".txt", "r") as f:
正在将文件加载到内存中,进行更改,然后以 "w+" 模式打开文件并将文件放回原处。
让我们在 foo 示例上执行此操作:
with open("foo.txt", 'r') as f: #open in read mode
a = f.read().split(',') #breaking it apart so i can locate the 6th number easier
a[6] = str(int(a[6]) + 2) #make my modifications
a = ','.join(a) #convert it back to string
with open("foo.txt", 'w+') as f: #open in write and delete everything
f.write(a) #write my modified version of the file
请注意,这是一个非常基本的示例,因此没有考虑换行符。
编辑:解决方案只是改变我打开文件的方式(感谢 Primusa),而不是我替换信息的方式。
我试图覆盖文件的一部分,但我的代码不起作用。当它 运行 时,它会完全删除文件中的所有内容,不留下任何东西。
这是我的代码:
for fname in store:
with open(fname + ".txt", "w+") as f:
for line in f:
c = store[fname]
x = (c-1)%len(line.split(","))
y = m.floor((c-1)/len(line.split(",")))
for n in range(y):
f.readline()
ldata = line.strip("\n").split(",")
for i in range(len(ldata)):
ldata[i] = int(ldata[i])
if w == None:
ldata[x] += 1
elif w == True:
ldata[x] += 2
elif w == False:
ldata[x] -= 1
#s = line.find("\n") - 1
#f.truncate(s)
f.write(str(ldata) + "\n")
密钥:
fname
:文件名对应的字符串变量,不带文件类型。store
:包含文件名键作为字符串和整数值的字典。f
: 包含多行整数列表的文件。c
:整型变量,用于确定要访问的整数。x
和y
:值设置为文件中要访问的整数的列和行(分别)的变量。w
:存储为布尔值或None
的变量,用于确定访问的整数是否应该增加或减少,以及增加多少。s
: 当前未使用的整数变量,用于 运行 分类文件的一部分。
使用示例:
假设我有一个文件 Foo.txt
,其中存储了以下信息:
0,2,3,7
11,3,6,4
我想将“6”值增加 2,所以我 运行 将 w
设置为 True
的代码并将 "Foo" : 7
添加到 store
,因为“6”是文件中的第 7 个数字(无论列表长度如何)。
应该发生什么:
Foo.txt
已修改,现在包含:
0,2,3,7
11,3,8,4
实际发生了什么:
Foo.txt
仍然存在,但现在包含:
也就是说它是空的。
我的代码有什么问题?我是否错误地处理了文件、变量计算、语法或其他完全不同的东西?
with open(fname + ".txt", "w+") as f:
以 "w+" 模式打开文件会截断文件,这意味着它会删除其中的所有内容。在这条语句之后你所做的所有操作都是在一个空文件上。
我建议以读取模式打开文件:
with open(fname + ".txt", "r") as f:
正在将文件加载到内存中,进行更改,然后以 "w+" 模式打开文件并将文件放回原处。
让我们在 foo 示例上执行此操作:
with open("foo.txt", 'r') as f: #open in read mode
a = f.read().split(',') #breaking it apart so i can locate the 6th number easier
a[6] = str(int(a[6]) + 2) #make my modifications
a = ','.join(a) #convert it back to string
with open("foo.txt", 'w+') as f: #open in write and delete everything
f.write(a) #write my modified version of the file
请注意,这是一个非常基本的示例,因此没有考虑换行符。