从旧文件读取数据、将其写入新文件然后删除 Python 中的旧文件时出错
Error reading data from the old files, writing it into the new files and then deleting the old files in Python
我正在尝试使用以下代码从源中读取 5 个文件,将它们写入目标,然后删除源中的文件。我收到以下错误:[Errno 13] Permission denied: 'c:\data\AM\Desktop\tester1
。顺便说一句,该文件如下所示:
import os
import time
source = r'c:\data\AM\Desktop\tester'
destination = r'c:\data\AM\Desktop\tester1'
for file in os.listdir(source):
file_path = os.path.join(source, file)
if not os.path.isfile:
continue
print(file_path)
with open (file_path, 'r') as IN, open (destination, 'w') as OUT:
data ={
'Power': None,
}
for line in IN:
splitter = (ID, Item, Content, Status) = line.strip().split()
if Item in data == "Power":
Content = str(int(Content) * 10)
os.remove(IN)
我不确定你在这里要做什么,但这是我可以提出的标题中的问题。
import os
# Takes the text from the old file
with open('old file path.txt', 'r') as f:
text = f.read()
# Takes text from old file and writes it to the new file
with open('new file path.txt', 'w') as f:
f.write(text)
# Removes the old text file
os.remove('old file path.txt')
从你的描述看来,这条线失败了:
with open (file_path, 'r') as IN, open (destination, 'w') as OUT:
因为这个操作:
open (destination, 'w')
因此,您可能没有对
的写入权限
c:\data\AM\Desktop\tester1
在 Windows 系统上设置文件权限:
https://www.online-tech-tips.com/computer-tips/set-file-folder-permissions-windows/
我重写了你的全部代码。我假设您想将 Power
的值更新为 10 的倍数,并将更新后的内容写入新文件。下面的代码将做到这一点。
您的代码有多个问题,首先,您脑海中想要的大部分内容都没有写入代码(例如写入新文件、提供写入内容和写入位置等)。最初的权限问题是因为您试图打开一个 directory
来写入而不是 file
.
source = r'c:\data\AM\Desktop\tester'
destination = r'c:\data\AM\Desktop\tester1'
for file in os.listdir(source):
source_file = os.path.join(source, file)
destination_file=os.path.join(destination, file)
if not os.path.isfile:
continue
print(source_file)
with open (source_file, 'r') as IN , open (destination_file, 'w') as OUT:
data={
'Power': None,
}
for line in IN:
splitter = (ID, Item, Content, Status) = line.strip().split()
if Item in data:# == "Power": #Changed
Content = str(int(Content) * 10)
OUT.write(ID+'\t'+Item+'\t'+Content+'\t'+Status+'\n') #Added to write the content into destination file.
else:
OUT.write(line) #Added to write the content into destination file.
os.remove(source_file)
希望这对你有用。
@Sherin Jayanand
还有一个问题兄弟,我想用你的一些代码尝试一下。我做到了:
import os
import time
from datetime import datetime
#Make source, destination and archive paths.
source = r'c:\data\AM\Desktop\Source'
destination = r'c:\data\AM\Desktop\Destination'
archive = r'c:\data\AM\Desktop\Archive'
for root, dirs, files in os.walk(source):
for f in files:
pads = (root + '\' + f)
# print(pads)
for file in os.listdir(source):
dst_path=os.path.join(destination, file)
print(dst_path)
with open(pads, 'r') as IN, open(dst_path, 'w') as OUT:
data={'Power': None,
}
for line in IN:
(ID, Item, Content, Status) = line.strip().split()
if Item in data:
Content = str(int(Content) * 10)
OUT.write(ID+'\t'+Item+'\t'+Content+'\t'+Status+'\n')
else:
OUT.write(line)
但我再次收到同样的错误:Permission denied: 'c:\data\AM\Desktop\Destination\C'
怎么来的?非常感谢你!
我正在尝试使用以下代码从源中读取 5 个文件,将它们写入目标,然后删除源中的文件。我收到以下错误:[Errno 13] Permission denied: 'c:\data\AM\Desktop\tester1
。顺便说一句,该文件如下所示:
import os
import time
source = r'c:\data\AM\Desktop\tester'
destination = r'c:\data\AM\Desktop\tester1'
for file in os.listdir(source):
file_path = os.path.join(source, file)
if not os.path.isfile:
continue
print(file_path)
with open (file_path, 'r') as IN, open (destination, 'w') as OUT:
data ={
'Power': None,
}
for line in IN:
splitter = (ID, Item, Content, Status) = line.strip().split()
if Item in data == "Power":
Content = str(int(Content) * 10)
os.remove(IN)
我不确定你在这里要做什么,但这是我可以提出的标题中的问题。
import os
# Takes the text from the old file
with open('old file path.txt', 'r') as f:
text = f.read()
# Takes text from old file and writes it to the new file
with open('new file path.txt', 'w') as f:
f.write(text)
# Removes the old text file
os.remove('old file path.txt')
从你的描述看来,这条线失败了:
with open (file_path, 'r') as IN, open (destination, 'w') as OUT:
因为这个操作:
open (destination, 'w')
因此,您可能没有对
的写入权限c:\data\AM\Desktop\tester1
在 Windows 系统上设置文件权限: https://www.online-tech-tips.com/computer-tips/set-file-folder-permissions-windows/
我重写了你的全部代码。我假设您想将 Power
的值更新为 10 的倍数,并将更新后的内容写入新文件。下面的代码将做到这一点。
您的代码有多个问题,首先,您脑海中想要的大部分内容都没有写入代码(例如写入新文件、提供写入内容和写入位置等)。最初的权限问题是因为您试图打开一个 directory
来写入而不是 file
.
source = r'c:\data\AM\Desktop\tester'
destination = r'c:\data\AM\Desktop\tester1'
for file in os.listdir(source):
source_file = os.path.join(source, file)
destination_file=os.path.join(destination, file)
if not os.path.isfile:
continue
print(source_file)
with open (source_file, 'r') as IN , open (destination_file, 'w') as OUT:
data={
'Power': None,
}
for line in IN:
splitter = (ID, Item, Content, Status) = line.strip().split()
if Item in data:# == "Power": #Changed
Content = str(int(Content) * 10)
OUT.write(ID+'\t'+Item+'\t'+Content+'\t'+Status+'\n') #Added to write the content into destination file.
else:
OUT.write(line) #Added to write the content into destination file.
os.remove(source_file)
希望这对你有用。
@Sherin Jayanand
还有一个问题兄弟,我想用你的一些代码尝试一下。我做到了:
import os
import time
from datetime import datetime
#Make source, destination and archive paths.
source = r'c:\data\AM\Desktop\Source'
destination = r'c:\data\AM\Desktop\Destination'
archive = r'c:\data\AM\Desktop\Archive'
for root, dirs, files in os.walk(source):
for f in files:
pads = (root + '\' + f)
# print(pads)
for file in os.listdir(source):
dst_path=os.path.join(destination, file)
print(dst_path)
with open(pads, 'r') as IN, open(dst_path, 'w') as OUT:
data={'Power': None,
}
for line in IN:
(ID, Item, Content, Status) = line.strip().split()
if Item in data:
Content = str(int(Content) * 10)
OUT.write(ID+'\t'+Item+'\t'+Content+'\t'+Status+'\n')
else:
OUT.write(line)
但我再次收到同样的错误:Permission denied: 'c:\data\AM\Desktop\Destination\C'
怎么来的?非常感谢你!