简体   繁体   English

从 csv 文件中读取数据,同时注意文件夹的更改

[英]Read the data from the csv file while keeping a watch on the folder for changes

I have a code which keeps a watch on a folder for any alterations.我有一个代码可以监视文件夹中的任何更改。 It checks for newly added and removed files and promptly displays the name of the file when such cases happen.它会检查新添加和删除的文件,并在发生此类情况时及时显示文件名。 This is my code:这是我的代码:

import os, time
import pandas as pd
import glob
path_to_watch = os.path.abspath('C:/Folder for violation csv/')
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
  time.sleep (2)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added: print ("Added: ", ", ".join (added))
  if removed: print ("Removed: ", ", ".join (removed))
  before = after

Now I want to include one more thing in this code, I want to read the data in the file which is being added in the folder.现在我想在这段代码中再包含一件事,我想读取正在添加到文件夹中的文件中的数据。 I tried it by converting the dictionary into a DataFrame using from_dict and then using pandas to read the csv using read_csv but it doesn't seem to be working.我尝试使用 from_dict 将字典转换为from_dict ,然后使用pandas使用read_csv读取 csv ,但它似乎不起作用。 This is my updated code:这是我更新的代码:

import os, time
import pandas as pd
import glob
path_to_watch = os.path.abspath('C:/Folder for violation csv/')
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
  time.sleep (2)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added: 
    print ("Added: ", ", ".join (added))
  df = pd.DataFrame.from_dict(path_to_watch, orient = 'index')
  df1 = pd.read_csv(df)
  if removed: print ("Removed: ", ", ".join (removed))
  before = after

If you could help me get the desired result, I'd greatly appreciate it.如果你能帮助我得到想要的结果,我将不胜感激。

  • To begin with, it is better to use the pathlib package for accessing the file system.首先,最好使用路径库 package来访问文件系统。
  • Use set to collect the file names.使用set收集文件名。
from pathlib import Path

path_to_watch = Path('C:/Folder for violation csv/')
before = set(path_to_watch.glob('**/*'))
while 1:
  time.sleep (2)
  after = set(path_to_watch.glob('**/*')
  added = after - before
  removed = before - after
  for f in added:
     df = pd.read_csv(f)
  before = after

try this,尝试这个,

import os, time
import pandas as pd
import glob
path_to_watch = os.path.abspath('C:/Folder for violation csv/')
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
  time.sleep (2)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added: 
    print ("Added: ", ", ".join (added))
  for file_ in added:
    df = pd.read_csv(os.path.join(path_to_watch, file_))
  if removed: print ("Removed: ", ", ".join (removed))
  before = after

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

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