How To Remove the desktop.ini file from dataframe when using os Module in Python

Question:

I’m super new to python and I’m creating a dataframe of files using the os module. I’ve seen another question about deleting the desktop.ini file, but I’m still learning how exactly to apply certain things to my code.

With the .ini file present, I can’t get a total for my third column. Here’s what I have:

import os
import pandas as pd

dir_path = r'C:Usersfilepathwhatever'
files = []

for path in os.listdir(dir_path):
if os.path.isfile(os.path.join(dir_path, path)):
    files.append(path)
    
labels = ["Invoice","Vendor","Amount"]
df = pd.DataFrame.from_records(dict(zip(labels, x.split(" "))) for x in files)
df['Amount'] = df['Amount'].str.rstrip('.pdf')

fund_totals={}

df.loc['Total'] = df.sum(numeric_only=True)
print(df)

Which will give me a dataframe like this:

14              bb             Expense   131.69
15     desktop.ini                 NaN      NaN
16              dh             Expense    60.98
Total          NaN                 NaN      NaN

How can I delete the desktop.ini file?

Asked By: SHW

||

Answers:

I haven’t dug deep but this suffices for a solution:

import os

dir_path = r'desktop_ini_files'
files = []

for path in os.listdir(dir_path):
    if os.path.isfile(os.path.join(dir_path, path)) and path != 'desktop.ini':
        files.append(path)
    
print (files)

Please let me know if it helps.

Answered By: Arijit Aich
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.