【问题标题】:Check if paths from file are physically located in hard drive检查文件中的路径是否物理位于硬盘驱动器中
【发布时间】:2014-10-22 18:13:33
【问题描述】:

您好,我在该文件中有一个名为 files.txt 的文件,其中包含文件路径,例如: /home/ojom/123.jpg /home/ojom/oaksdokwijeqijwqe.jpg

该文件中有数百万条路径我需要查看该文件中的文件是否物理位于(存在)在我的硬盘驱动器上(如果它们没有将这些路径写入另一个文件)我该怎么做?我可以使用什么?

【问题讨论】:

  • 任何你喜欢的编程语言。

标签: disk


【解决方案1】:

您可以使用 PHP 解析该文件,然后查看结果并使用 file_exists 检查它们。

如果每个文件路径都在新行上,则以下示例有效。

<?php

$files = array();
$handle = fopen("files.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if(!file_exits($line)) {
          continue; // file does not exist, skip
        } else {
          $files[] = $line;
        }
    }
} else {
    die('Error opening the file');
} 
fclose($handle);

echo "These files exist:";
echo "<pre>" . print_r($files, true) . "</pre>"; // prints them as an array

您还可以使用该数组进行进一步处理。

【讨论】:

    【解决方案2】:

    这里是Python 解决方案:

    import os.path
    
    files = 'c:\\test\\files.txt'
    output = 'c:\\test\\filesNotExist.txt'
    
    with open(files) as f:
        for file in f:
            if not os.path.isfile(file):
                f = open(output, 'w')
                f.write(file)
                f.close()
    f.close()
    

    此脚本扫描您的文本文件并将不存在的文件列表写入输出文本文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 2017-12-31
      • 1970-01-01
      • 2011-07-18
      相关资源
      最近更新 更多