无法将列添加到现有的csv文件以及如何在csv文件中调用函数

时间:2019-09-27 09:41:55

标签: python pandas csv

如何通过将整个列名称添加到“原始图像名称”列中来追加csv文件?

enter image description here

import os
import pandas as pd

def getListOfFiles(dirName):

    listOfFile = os.listdir(dirName)
    allFiles = list()

    # Iterate over all the entries
    for entry in listOfFile:

        # Create full path
        fullPath = os.path.join(dirName, entry)

        # If entry is a directory then get the list of files in this directory
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles


def main():
    dirName = '/home/ushasri/Downloads/Training'

    # Get the list of all files in directory tree at given path
    listOfFiles = getListOfFiles(dirName)

    # Print the files
    for elem in listOfFiles:
        # print(elem)
        # print ("****************")

        # Get the list of all files in directory tree at given path
        listOfFiles = list()

    for (dirpath, dirnames, filenames) in os.walk(dirName):
        listOfFiles += [os.path.join(file) for file in filenames]

    # Print the files
    x = 0
    for elem in listOfFiles:
        print(elem)
        data = pd.read_csv(r'images.csv')
        df = pd.DataFrame(data, columns=["Original Image Directory path", "Original Image Name", "Category",
                                         "New Image Directory Path", "New Image Name"])
        # print(df)

        df2 = pd.DataFrame(elem, columns=['Original Image Name'])
        df.append(df2)
        # print(df)
        df.to_csv("images.csv")



if __name__ == '__main__':
    main()

Image is posted below

0 个答案:

没有答案