检查文件路径是否在物理上位于硬盘驱动器
问题描述:
你好我有一个名为files.txt的文件在那个文件中有文件路径例如: /home/ojom/123.jpg /home/ojom/oaksdokwijeqijwqe .jpg检查文件路径是否在物理上位于硬盘驱动器
这个文件中有数百个这样的路径,我需要看看这个文件中的文件是否在我的硬盘上物理存在(如果它们不会将这些路径写入另一个文件)怎么办我这样做?我可以使用什么?
答
你可以使用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
您也可以使用该数组进行进一步处理。
答
这里是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()
这个脚本会扫描你的文本文件,并把不存在的文件列表输出文本文件。
您喜欢的任何编程语言。 – Quentin 2014-10-22 09:47:10