在Python中从旧文件中读取数据,将其写入新文件,然后删除旧文件时出错。

问题描述 投票:0回答:1

我试图使用下面的代码从源文件中读取5个文件,将它们写入目标文件,然后删除源文件。我得到以下错误。[Errno 13] Permission denied: 'c:\\data\\AM\\Desktop\\tester1. 顺便说一下,文件是这样的。enter image description here

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)
python file move
1个回答
1
投票

我已经重新写了你的整个代码。我想你是想更新 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)

希望这对你有用


0
投票

我不知道你要在这里做什么,但这是我能想到的问题放到标题中。

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')

0
投票

从你的描述中听起来,这一行是失败的。

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.comcomputer-tipsset-file-folder-permissions-windows


0
投票

@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'

怎么会这样?非常感谢你

© www.soinside.com 2019 - 2024. All rights reserved.