【问题标题】:how to get a folder name and file name in python如何在python中获取文件夹名和文件名
【发布时间】:2016-08-08 10:40:36
【问题描述】:

我有一个名为myscript.py 的python 程序,它会为我提供所提供路径中的文件和文件夹列表。

import os
import sys

def get_files_in_directory(path):
    for root, dirs, files in os.walk(path):
        print(root)
        print(dirs)
        print(files)
path=sys.argv[1]
get_files_in_directory(path)

我提供的路径是D:\Python\TEST,其中有一些文件夹和子文件夹,您可以在下面提供的输出中看到:

C:\Python34>python myscript.py "D:\Python\Test"
D:\Python\Test
['D1', 'D2']
[]
D:\Python\Test\D1
['SD1', 'SD2', 'SD3']
[]
D:\Python\Test\D1\SD1
[]
['f1.bat', 'f2.bat', 'f3.bat']
D:\Python\Test\D1\SD2
[]
['f1.bat']
D:\Python\Test\D1\SD3
[]
['f1.bat', 'f2.bat']
D:\Python\Test\D2
['SD1', 'SD2']
[]
D:\Python\Test\D2\SD1
[]
['f1.bat', 'f2.bat']
D:\Python\Test\D2\SD2
[]
['f1.bat']

我需要以这种方式获取输出:

D1-SD1-f1.bat
D1-SD1-f2.bat
D1-SD1-f3.bat
D1-SD2-f1.bat
D1-SD3-f1.bat
D1-SD3-f2.bat
D2-SD1-f1.bat
D2-SD1-f2.bat
D2-SD2-f1.bat

我如何以这种方式获得输出。(请记住,这里的目录结构只是一个示例。该程序应该对任何路径都灵活)。我该怎么做呢。 有没有为此的任何 os 命令。你能帮我解决这个问题吗? (附加信息:我使用的是 Python3.4)

【问题讨论】:

    标签: python python-3.x path


    【解决方案1】:

    您可以尝试改用glob 模块:

    import glob
    glob.glob('D:\Python\Test\D1\*\*\*.bat')
    

    或者,只是获取文件名

    import os
    import glob
    [os.path.basename(x) for x in glob.glob('D:\Python\Test\D1\*\*\*.bat')]
    

    【讨论】:

      【解决方案2】:

      要获得您想要的,您可以执行以下操作:

      def get_files_in_directory(path):
          # Get the root dir (in your case: test)
          rootDir = path.split('\\')[-1]
      
          # Walk through all subfolder/files
          for root, subfolder, fileList in os.walk(path):
              for file in fileList:
                  # Skip empty dirs
                  if file != '':
                      # Get the full path of the file
                      fullPath = os.path.join(root,file)
      
                      # Split the path and the file (May do this one and the step above in one go
                      path, file = os.path.split(fullPath)
      
                      # For each subfolder in the path (in REVERSE order)
                      subfolders = []
                      for subfolder in path.split('\\')[::-1]:
      
                          # As long as it isn't the root dir, append it to the subfolders list
                          if subfolder == rootDir:
                              break
                          subfolders.append(subfolder)
      
                      # Print the list of subfolders (joined by '-')
                      # + '-' + file
                      print('{}-{}'.format( '-'.join(subfolders), file) )
      
      path=sys.argv[1]
      get_files_in_directory(path)
      

      我的测试文件夹:

      SD1-D1-f1.bat
      SD1-D1-f2.bat
      SD2-D1-f1.bat
      SD3-D1-f1.bat
      SD3-D1-f2.bat
      

      这可能不是最好的方法,但它会得到你想要的。

      【讨论】:

      • 非常感谢。真的很有帮助
      • @SushinK.Kumar:那么你应该接受这个而不是添加过时的评论。
      猜你喜欢
      • 2016-01-27
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多