从文本文件读取路径

时间:2019-03-05 23:08:36

标签: python arrays os.walk

我编写了一个脚本,该脚本将扫描共享文件夹并获取这些文件夹中所有文件的文件年龄以及一些其他文件元信息。

<Window x:Class="Container.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="700" Width="1000">
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="10*" />
        </Grid.ColumnDefinitions>

    </Grid>
</Window>

我必须对代码中的所有共享进行硬编码,而不是考虑使脚本从可更新所有路径的文本文件中读取。

my_paths = []
for p in ['\\\\data\\hold\\app1','\\\\data\\hold\\app2']:
    for dirpath, dirnames, filenames in os.walk(p):
        my_paths.append(filenames)
        for filename in filenames:
            full_path = os.path.join(dirpath, filename)
            if last_modified(full_path) < threshold:
                string_output='Age :' +age_file(full_path)+'\n'+'File Size : '+file_size(full_path)+'\n'
                print(string_output)
                #create(string_output);

更新代码

text.txt

\\data\hold\app1
\\data\hold\app2

my_paths返回数据,例如['\\ data \ hold \ app1','\\ data \ hold \ app2'],但其余部分无法正常工作。

1 个答案:

答案 0 :(得分:0)

您只是忘记了删除循环中读取的行:

...
for dirpath, dirnames, filenames in os.walk(line.strip()):
...