如何在Python中循环文件以识别和计算重复项?

时间:2017-07-26 07:30:07

标签: python

我有一堆文件,我想记录文件发生的实例数。以下是我拥有的数据示例:

Sample of the data

我希望代码在一个单独的excel文件中运行所有这些文件并记录输出:

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsESRv.CSVWriter;

Boolean result=true;
public WebDriver driver;
try
{
     System.setProperty("webdriver.chrome.driver","Projects\\chromedriver.exe");
     driver=new ChromeDriver();

     driver.manage().window().maximize();
    driver.get("https://something.com/registration/");
     WebDriverWait wait = new WebDriverWait(driver, 20000);

     String uname= bsh.args[0];
     String pass= bsh.args[1];

      // Logged In 
     driver.findElement(By.xpath("//*[contains(@id,'menu-item')]//*[text()='Log In']")).click();
     driver.findElement(By.id("username")).sendKeys(new String[] {uname});
     driver.findElement(By.id("password")).sendKeys(new String[] {pass});
     driver.findElement(By.xpath("//*[@id='woocommerce-login-nonce']/following-sibling::*[@name='login']")).click();

     // Logged Out
     driver.findElement(By.xpath("//*[@id='masthead']//*[text()='Sign out']")).click();
}
catch (Exception ex) 
{
    ex.printStackTrace();
    IsSuccess = false;
    ResponseCode = "500";
    ResponseMessage = ex.getMessage();
    log.error(ex.getMessage());
    System.err.println(ex.getMessage());
} 
catch (Throwable thex) 
{
    System.err.println(thex.getMessage());
}
finally 
{
    driver.quit();
}
IsSuccess=result;
return result;

有没有办法可以使用python脚本来完成?

1 个答案:

答案 0 :(得分:-2)

你可以创建类似的东西:

import os
d = {}
for current_dir in [dir1, dir2, dir3..]  # iterates over directories with files
    files = os.listdir(current_dir)

    for fi in files:
        if fi in d.keys():
            d[fi] += 1  # it is already there, let's increment
        else:
           d[fi] = 1  # it is a new file

for k, v in d.items():  # let's print the file stats
    print 'File:', k, 'count':' v