使用os.walk()引用USB驱动器不会返回任何文件

时间:2016-02-06 22:04:53

标签: python tkinter

当我使用os.walk('/media/pi/UNTITLED')引用插入我的rasberry pi的USB驱动器时它什么都没有返回,但当我在脚本(os.walk('./movies'))的同一文件夹中引用某些内容时,它工作正常。我也没有收到错误消息。这是权限吗?我使用的路径不正确吗?我可以导航到终端中的USB驱动器。请参阅下面的代码了解我的整个脚本。我再次为长度道歉,但我不确定我哪里出错了。

谢谢你, 标记

from tkinter import *
from tkinter import ttk
import os
import os.path
import re
import fnmatch
import math

class scareGUI:

    def __init__(self, master):

        self.master = master
        tfLogo = PhotoImage(file='tf_logo.gif')
        scareBtn = PhotoImage(file='scare_btn.gif')
        scareBtnStop = PhotoImage(file='scare_btn_stop.gif')

        s = ttk.Style()
        s.configure('cbfont.TCheckbutton',font=('Courier',16))

        #self.selectedFileName = StringVar()

        #filter non video file types
        includes = ['*.mp4']
        includes = r'|'.join([fnmatch.translate(x) for x in includes])

        master.attributes('-zoomed', True)

        logoLabel=ttk.Label(self.master,text="TerrorForm ScareBox", font=("Helvetica", 24))
        logoLabel.config(image=tfLogo)
        logoLabel.pack(fill=BOTH)
        logoLabel.image=tfLogo
        directionLabel = ttk.Label(self.master,text="Select the files below you would like the ScareBox to play.", font=("Courier",20,'bold'))
        directionLabel.pack(fill=BOTH, padx=10, pady=20)

        ttk.Separator(self.master,orient='horizontal').pack(side="top",fill=X,padx=20, pady=20)

        self.frame_files=ttk.Frame(self.master)
        self.frame_files.pack(fill=X, padx=20)

        ttk.Separator(self.master,orient='horizontal').pack(side="top",fill=X,padx=20,pady=20)

        contVar=IntVar()
        playContinuousBtn = ttk.Checkbutton(self.master,text='Play movies in a loop without motion detection',style='cbfont.TCheckbutton',variable=contVar)

        playContinuousBtn.pack(side=TOP,padx=20, pady=20)

        frame_buttons = ttk.Frame(self.master)
        frame_buttons.pack(side=TOP)

        stopButton = ttk.Button(frame_buttons, text="I'm Affraid", command = master.destroy)
        stopButton.config(image=scareBtnStop)
        stopButton.config(compound=TOP)
        stopButton.image=scareBtnStop
        stopButton.pack(side=LEFT,fill=Y,padx=10, pady=10)

        closeButton = ttk.Button(frame_buttons, text="Start Scaring!", command = master.destroy)
        closeButton.config(image=scareBtn)
        closeButton.config(compound=TOP)
        closeButton.image=scareBtn
        closeButton.pack(side=LEFT,fill=Y,padx=10, pady=10)

        #pull file names from file system
        #for root, dirs, self.files in os.walk('./movies'):
        for root, dirs, self.files in os.walk('/media/pi/UNTITLED'):

            self.files = [os.path.join(root, f) for f in self.files]
            self.files = [f for f in self.files if re.match(includes, f)]

        #Determine number of rows in grid
        numfiles = len(self.files)

        dividesBy = False
        numRows = IntVar()
        numColumns = 4
        while (dividesBy == False):
            if(numfiles % numColumns == 0):
                numRows=int(numfiles/numColumns)
                dividesBy = True
            else:
                numfiles+=1


        #Print Check Boxes in Grid layout
        filmIcon = PhotoImage(file='tf_film_icon.gif')
        i=0
        self.checkedFiles=[None]*len(self.files)


        for r in range(numRows):
            for c in range(4):
                if (i<len(self.files)):
                   self.checkedFiles[i] = IntVar()
                   checkbutton = ttk.Checkbutton(self.frame_files, text=self.files[i], variable=self.checkedFiles[i])
                   checkbutton.config(image = filmIcon,compound = TOP)
                   checkbutton.image = filmIcon
                   checkbutton.grid(row = r, column = c)
                   i+=1


    def getChosenFiles(self):
        self.chosenFiles=[]
        for x in range(len(self.files)):
            if(self.checkedFiles[x].get()):
                    self.chosenFiles.append(self.files[x])
        return self.chosenFiles

    def main():
        root = Tk()
        scaregui = scareGUI(root)
        root.mainloop()

if __name__ == "__main__": main()

2 个答案:

答案 0 :(得分:0)

我认为includes可能会弄乱事情。我建议制作一个小代码片段来测试您是否可以访问该特定目录。也许是这样的:

import os

includes = ('.mp4',)

movie_list = []
for root, dirs, files in os.walk('/media/pi'):
    files = [f for f in files if f.endswith(includes)]
    movie_list.extend([os.path.join(root, f) for f in files])

print('number of files: ', len(movie_list))

这将列出以.mp4结尾的所有文件,并打印找到的文件数。如果您希望将movie_list存储在类可访问的变量中,则self.movie_list可以是<TextBox x:Class="HelloWorld.ElphantTextBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HelloWorld" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Style="{DynamicResource ElphantTextBoxStyle}" Text="Hello"> <TextBox.Resources> <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/> <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/> <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/> <Style x:Key="ElphantTextBoxStyle" TargetType="{x:Type TextBox}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Border x:Name="border" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" BorderBrush="{x:Null}" Visibility="Visible" VerticalAlignment="Stretch" Height="Auto"> <Canvas x:Name="canvas" Margin="-295.367,-223.734,-296,-226" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Path Width="Auto" Height="Auto" Canvas.Left="219.602" Canvas.Top="498.103" Stretch="Fill" StrokeThickness="12" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeMiterLimit="2.75" Stroke="#FF1F28C4" Fill="#FFFFFFFF" Data="F1 M 590.49,425.244C 590.49,441.313 592.997,457.296 594.99,473.241C 596.244,483.275 607.718,497.363 599.49,503.24C 592.526,508.214 582.034,503.315 573.991,506.24C 558.948,511.71 541.69,507.879 525.994,504.74C 524.033,504.348 521.994,504.74 519.994,504.74C 517.494,504.74 514.665,505.98 512.494,504.74C 506.041,501.052 507.853,490.602 504.995,483.741C 499.743,471.135 495.127,458.18 488.496,446.243C 485.33,440.544 484.1,432.032 477.996,429.743C 463.952,424.477 447.906,431.4 432.998,429.743C 398.697,425.932 363.845,423.31 329.504,426.744C 322.96,427.398 312.945,423.862 310.004,429.743C 308.864,432.024 310.772,434.801 311.504,437.243C 312.278,439.822 313.731,442.164 314.504,444.743C 318.167,456.952 320.596,469.572 322.004,482.241C 322.232,484.29 323.004,486.241 323.504,488.241C 324.004,490.24 326.147,492.525 325.004,494.24C 314.742,509.632 290.646,508.611 272.506,512.239C 260.648,514.611 248.541,515.536 236.508,516.739C 233.971,516.993 231.427,517.433 229.009,518.239C 226.887,518.946 225.13,521.946 223.009,521.239C 204.761,515.156 214.009,483.476 214.009,464.242C 214.009,442.22 210.045,419.137 217.009,398.245C 223.232,379.577 235.208,363.348 244.008,345.748"/> <Path Width="Auto" Height="Auto" Canvas.Left="251.521" Canvas.Top="219.409" Stretch="Fill" StrokeThickness="12" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeMiterLimit="2.75" Stroke="#FF1F28C4" Fill="#FF528CCE" Data="F1 M 290.505,176.256C 304.397,141.527 345.545,123.988 379.001,107.26C 411.683,90.9188 447.68,79.7963 483.996,75.7612C 542.666,69.2424 607.247,57.3338 660.987,81.7609C 679.108,89.9977 701.877,92.2796 714.984,107.26C 735.65,130.878 746.835,161.345 762.982,188.256C 779.781,216.254 797.897,244.877 804.98,276.751C 807.066,286.139 806.716,296.038 809.479,305.25C 810.317,308.042 812.897,310.043 813.979,312.749C 814.926,315.116 815.198,317.715 815.479,320.249C 817.633,339.635 816.979,359.241 816.979,378.746C 816.979,415.909 821.573,455.78 806.48,489.741C 798.923,506.744 791.138,524.581 777.981,537.738C 768.273,547.446 754.906,552.622 743.483,560.237C 733.352,566.991 720.87,569.595 708.985,572.236C 703.128,573.538 695.228,576.479 690.985,572.236C 675.096,556.347 674.877,530.28 666.987,509.24C 657.717,484.521 648.632,459.209 633.988,437.243C 627.139,426.969 621.034,416.216 614.489,405.745C 613.304,403.849 613.489,400.745 611.489,399.745C 609.646,398.823 607.139,400.008 605.49,401.245C 602.929,403.166 601.617,406.352 599.49,408.745C 593.613,415.356 587.067,421.436 579.991,426.744C 563.47,439.134 539.145,434.243 518.494,434.243C 499.26,434.243 479.744,431.326 461.497,425.244C 451.313,421.849 442.382,415.458 432.998,410.244C 431.044,409.159 428.58,405.664 426.999,407.245C 420.635,413.608 426.999,425.244 426.999,434.243C 426.999,458.268 436.786,481.287 441.998,504.74C 444.944,517.999 448.104,536.753 437.498,545.238C 426.608,553.949 410.838,554.007 397,555.737C 384.508,557.299 372.091,560.237 359.502,560.237C 345.467,560.237 327.428,567.161 317.504,557.237C 309.913,549.646 307.076,538.452 302.505,528.739C 289.894,501.941 279.628,474.076 269.507,446.243C 264.302,431.929 256.324,418.694 251.507,404.245C 243.405,379.939 244.008,353.37 244.008,327.749C 244.008,312.177 241.544,295.179 248.508,281.251"/> <Path Width="Auto" Height="Auto" Canvas.Top="270.971" Stretch="Fill" StrokeThickness="12" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeMiterLimit="2.75" Stroke="#FF1F28C4" Fill="#FF528CCE" Data="F1 M 263.507,222.754C 248.276,215.138 222.727,206.131 212.509,219.754C 200.969,235.142 196.756,258.741 203.51,276.751C 212.009,299.416 237.758,313.477 260.507,321.749C 271.476,325.738 284.168,332.083 295.005,327.749C 297.978,326.56 299.978,323.714 302.505,321.749C 304.478,320.214 306.425,318.636 308.505,317.249C 320.654,309.149 334.515,302.133 343.003,290.25C 348.532,282.51 344.503,271.265 344.503,261.752C 344.503,241.597 342.196,221.229 337.003,201.755C 334.926,193.965 336.553,185.404 334.003,177.756C 326.25,154.495 311.161,127.21 287.506,120.759C 273.991,117.073 259.516,119.259 245.508,119.259C 199.204,119.259 140.222,136.341 119.514,177.756C 105.758,205.268 107.515,238.492 107.515,269.252C 107.515,318.29 112.153,368.374 101.515,416.244C 99.4513,425.53 103.023,435.718 100.015,444.743C 95.419,458.531 90.7929,473.464 80.516,483.741C 73.2277,491.029 61.4008,491.475 52.0174,495.74C 39.3381,501.504 25.9509,505.567 13.0194,510.739C 6.70534,513.265 -2.2315,511.429 -6.47967,516.739C -12.4875,524.249 1.68498,534.236 7.01965,542.238C 16.2387,556.067 28.5399,567.854 41.5179,578.236C 48.2008,583.582 59.362,585.063 67.0167,581.236C 74.9283,577.28 79.5335,568.667 86.5157,563.237C 106.058,548.037 131.318,535.226 140.513,512.239C 146.111,498.243 145.013,482.316 145.013,467.242C 145.013,425.098 136.665,378.942 155.512,341.248C 164.422,323.427 193.06,327.571 212.509,323.249C 219.058,321.794 224.509,317.249 230.508,314.249"/> <Path Width="Auto" Height="Auto" Canvas.Left="731.92" Canvas.Top="255.618" Stretch="Fill" StrokeThickness="12" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeMiterLimit="2.75" Stroke="#FF1F28C4" Fill="#00528CCE" Data="F1 M 723.984,141.758C 745.26,136.439 763.176,121.694 783.981,114.759C 796.752,110.502 810.207,100.003 822.979,104.26C 825.35,105.05 827.097,107.113 828.979,108.76C 831.107,110.622 832.58,113.26 834.978,114.759C 849.148,123.615 862.852,137.445 866.477,153.757C 870.071,169.93 872.148,190.177 861.977,203.255C 850.974,217.402 832.261,223.96 815.479,230.253C 801.326,235.561 789.357,245.799 777.981,255.752C 764.202,267.809 750.454,282.877 746.483,300.75C 743.119,315.888 744.983,331.74 744.983,347.248"/> <Viewbox Width="Auto" Height="Auto" Canvas.Left="186.451"> <Canvas Width="437.485" Height="445.914"> <Ellipse Width="401.485" Height="232.958" Canvas.Left="18" Canvas.Top="18" Stretch="Fill" StrokeThickness="12" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeMiterLimit="2.75" Stroke="#FF05040A" Fill="#FFFFFFFF"/> <Path Width="159.813" Height="205.605" Canvas.Left="156.061" Canvas.Top="218.36" Stretch="Fill" StrokeThickness="12" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeMiterLimit="2.75" Stroke="#FF05040A" Fill="#FFFFFFFF" Data="F1 M 227.623,241.213C 227.623,281.204 243.201,327.142 222.006,361.055C 213.533,374.611 198.53,383.251 184.555,391.015C 182.115,392.371 179.679,393.78 177.065,394.76C 174.655,395.664 172.072,396.008 169.575,396.633C 167.078,397.257 161.662,395.967 162.085,398.505C 164.2,411.198 185.179,410.364 197.663,413.485C 222.073,419.588 254.772,421.914 272.564,404.123C 284.619,392.068 289.463,373.952 293.162,357.31C 295.777,345.542 304.774,335.566 306.269,323.604C 309.444,298.21 312.476,271.658 306.269,246.831C 304.301,238.958 298.875,232.232 296.907,224.36"/> </Canvas> </Viewbox> <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Canvas.Left="257.937" Canvas.Top="76.395" Height="111.96" Width="290"/> </Canvas> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Opacity" TargetName="border" Value="0.56"/> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/> </Trigger> <Trigger Property="IsKeyboardFocused" Value="true"> <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/> <Condition Property="IsSelectionActive" Value="false"/> </MultiTrigger.Conditions> <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> </MultiTrigger> </Style.Triggers> </Style> </TextBox.Resources>

答案 1 :(得分:0)

确定。所以我解决了自己的问题。我没有将文件名附加到循环底部的列表中。在我的初始测试中我没有子目录的事实导致我错误地认为它正在工作。我的解决方案代码如下。

matches=[]
for root, dirs, self.files in os.walk('/media/pi'):
    for filename in self.files:
        if filename.endswith(('.mp4','.mov','.avi')):
            matches.append(os.path.join(root,filename))