繁体   English   中英

关闭副本文件但删除数据

[英]shutil copy files but deletes data

以下代码有时有效,将其推送到所需的目录中,IT总是复制该文件,但是有时leadleadser目录中的csv文件中没有数据。

import csv
import re
import os
import shutil


class myExporter(object):

    def __init__(self):
        self.i = 0
        self.filename = 'output%s.csv'
        self.srcfile = '/Users/poweruser/Applications/pythonwork/bbbscrap2/scrape/' + self.filename
        while os.path.exists(self.srcfile % self.i):
            self.i += 1
        self.folderdes = '/Users/poweruser/Applications/pythonwork/leadparser/newfiles'
        self.myCSV = csv.writer(open(self.filename % self.i,  'w'))
        self.myCSV.writerow(['Email', 'Website', 'Phone Number', 'Location'])

    def process_item(self, item, spider):
        self.myCSV.writerow([item['email'],
                             item['website'],
                             item['phonenumber'],
                             item['location']])
        self.folderPath = os.path.join(
            self.folderdes, os.path.basename(self.srcfile % self.i))
        shutil.copy(self.srcfile % self.i, self.folderPath)

        return item

您必须关闭文件以获取所有数据:

import csv
import re
import os
import shutil

SRCFILE = '/Users/poweruser/Applications/pythonwork/bbbscrap2/scrape/output%s.csv'
DESTINATION_FOLDER = '/Users/poweruser/Applications/pythonwork/leadparser/newfiles'

class myExporter(object):

    def __init__(self):
        i = 0
        while os.path.exists(SRCFILE % i):
            i += 1
        self.filename = SRCFILE % i
        with open(self.filename, 'w') as output:
            output = csv.writer(output)
            output.writerow(['Email', 'Website', 'Phone Number', 'Location'])

    def process_item(self, item, spider):
        with open(self.filename, 'a') as output:
            output = csv.writer(output)
            output.writerow([item['email'],
                             item['website'],
                             item['phonenumber'],
                             item['location']])
        folder = os.path.join(DESTINATION_FOLDER, os.path.basename(self.filename))
        shutil.copy(self.filename, folder)
        return item

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM