如何从目录中输入多个文件

时间:2015-10-09 00:20:56

标签: python xml unix

首先,我最近刚接触Unix,我试图在线找到我的问题的解决方案,但我找不到解决方案。

所以我通过我的Unix终端运行Python,我有一个解析xml文件并将结果输入.dat文件的程序。

我的程序有效,但我必须单独输入每个xml个文件(号码超过50)。

例如:

clamshell: python3 my_parser2.py 'items-0.xml' 'items-1.xml' 'items-2.xml' 'items-3.xml' .....`

所以我想知道是否可以从目录中读取,其中包含我的所有文件到我的程序中?而不是单独键入所有xml文件名并以这种方式运行程序。

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

python3 my_parser2.py *.xml应该有用。

答案 1 :(得分:1)

shell本身可以扩展通配符,因此,如果您不关心输入文件的顺序,只需使用:

python3 my_parser2.py items-*.xml

如果数字顺序 很重要(您希望{1}},0..9等按此顺序,您可能需要稍微调整通配符参数以保证这一点,例如:

10-99

答案 2 :(得分:1)

除了命令行选项之外,您可以在脚本中使用glob并绕过命令参数的需要:

<?php
/* set some options */
$mapLat = filter_input(INPUT_POST, 'lat1'); // latitude for map's and circle's center
$mapLng = filter_input(INPUT_POST, 'lon1'); // longitude for map's and circle's center
$mapRadius1 = 0.5; // the radius of the first circle (in Kilometres)
$mapRadius2 = 5; // the radius of the second circle (in Kilometres)
$mapFill_first = '330000'; // fill colour of the first circle
$mapFill_second = 'FF99FF'; // fill colour of the second circle
$map1Border1 = '91A93A'; // border colour of the first circle
$map1Border2 = '0000CC'; // border colour of the second circle
$mapWidth = 450; // map image width (max 640px)
$mapHeight = 450; // map image height (max 640px)
$zoom = 11;
$scale = 2;
/** create our encoded polyline string for the first circle*/
$EncString1 = GMapCircle($mapLat, $mapLng, $mapRadius1);
/** create our encoded polyline string for the second circle*/
$EncString2 = GMapCircle($mapLat, $mapLng, $mapRadius2);
/** put together the static map URL */
$MapAPI = 'http://maps.google.com.au/maps/api/staticmap?';
$MapURL = $MapAPI . 'center=' . $mapLat . ',' . $mapLng . '&zoom=' . $zoom . '&size=' .
    $mapWidth . 'x' . $mapHeight . '&scale=' . $scale . '&markers=color:red%7Clabel:S%7C'.$mapLat.','.$mapLng .
    '&maptype=roadmap&path=fillcolor:0x' . $mapFill_first .
    '33%7Ccolor:0x' . $map1Border1 . '00%7Cenc:' . $EncString1 . '&path=fillcolor:0x' .
    $mapFill_second . '33%7Ccolor:0x' . $map1Border2 . '00%7Cenc:' . $EncString2;

/* output an image tag with our map as the source */
//echo '<img src="' . $MapURL . '" />';
echo json_encode($MapURL);

function GMapCircle($Lat, $Lng, $Rad, $Detail = 8)
{
    $R = 6371;
    $pi = pi();
    $Lat = ($Lat * $pi) / 180;
    $Lng = ($Lng * $pi) / 180;
    $d = $Rad / $R;
    $points = array();
    for ($i = 0; $i <= 360; $i += $Detail)
    {
        $brng = $i * $pi / 180;
        $pLat = asin(sin($Lat) * cos($d) + cos($Lat) * sin($d) * cos($brng));
        $pLng = (($Lng + atan2(sin($brng) * sin($d) * cos($Lat), cos($d) - sin($Lat) * sin($pLat))) * 180) / $pi;
        $pLat = ($pLat * 180) / $pi;
        $points[] = array($pLat, $pLng);
    }

    require_once('PolylineEncoder.php');
    $PolyEnc = new PolylineEncoder($points);
    $EncString = $PolyEnc->dpEncode();

    return $EncString['Points'];
}

这将返回运行脚本的目录中的所有import glob filenames = glob.glob("*.xml") 个文件(作为文件名)。

然后,如果需要,您可以使用基本循环遍历所有文件:

.xml

答案 3 :(得分:1)

import glob

listOffiles = glob.glob('directory/*.xml')
相关问题