我想用Python编写一个脚本,用于存储图像文件名列表并将其打印到文本文件中

时间:2016-04-17 18:41:56

标签: python command-line

我创建了一个'JPG'文件夹并将该图像列表与python代码一起放在该文件夹中但是当我尝试运行此代码时,它打印出'./imagename.jpg'但我只想'imagename.jpg'在文本文件中。

import os
def GetFiles(dir,f):
    basedir = dir
    subdirs = []
    for fname in os.listdir(dir):
        fileName = os.path.join(basedir, fname)
        if os.path.isfile(fileName):
            if fileName.endswith(".jpg"):
                f.write(fileName+"\n")
                print fileName
        elif os.path.isdir(fileName):
            subdirs.append(fileName)
    for subdir in subdirs:
        GetFiles(subdir,f)
f=open("fileName.jpg",'w')
GetFiles('./',f)
f.close()

2 个答案:

答案 0 :(得分:0)

FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".jpg");
StorageFile file = await picker.PickSingleFileAsync();

if (file != null)
{
    grid.Background = new ImageBrush
    {
        ImageSource = new BitmapImage(new Uri(file.Path, UriKind.Absolute))
    };
}

答案 1 :(得分:0)

在文本文件中保存文件名或打印文件名之前,请运行:

file_name

请注意,根据PEP 8 on variables naming,您应该写 fileName代替dictionary_name = {accesie:organisme}