Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dir_path = r'C:\Users\file\path\whatever'
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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading