![](/img/trans.png)
[英]How to fix PermissionError in python when trying to write on desktop.ini file?
[英]How To Remove the desktop.ini file from dataframe when using os Module in Python
我是 python 的超級新手,我正在使用 os 模塊創建一個 dataframe 文件。 我已經看到另一個關於刪除 desktop.ini 文件的問題,但我仍在學習如何將某些東西准確地應用到我的代碼中。
存在 .ini 文件時,我無法獲得第三列的總數。 這是我所擁有的:
導入操作系統導入 pandas 作為 pd
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)
這會給我一個 dataframe 像這樣:
14 bb Expense 131.69
15 desktop.ini NaN NaN
16 dh Expense 60.98
Total NaN NaN NaN
如何刪除 desktop.ini 文件?
我還沒有深入挖掘,但這足以解決問題:
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)
如果有幫助,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.