2014-10-22 58 views
0

你好我有一個名爲files.txt的文件在那個文件中有文件路徑例如: /home/ojom/123.jpg /home/ojom/oaksdokwijeqijwqe .jpg檢查文件路徑是否在物理上位於硬盤驅動器

這個文件中有數百個這樣的路徑,我需要看看這個文件中的文件是否在我的硬盤上物理存在(如果它們不會將這些路徑寫入另一個文件)怎麼辦我這樣做?我可以使用什麼?

+1

您喜歡的任何編程語言。 – Quentin 2014-10-22 09:47:10

回答

0

你可以使用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 

您也可以使用該數組進行進一步處理。

0

這裏是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() 

這個腳本會掃描你的文本文件,並把不存在的文件列表輸出文本文件。

相關問題