我是一名模型风险分析师,我的日常工作涉及处理模型库存数据的大量治理,为此我最终使用 excel 表和 word 文档来处理任何事情。今天我将讨论一些我用来在 Python 中自动执行大部分 Excel 任务的函数。让我们开始!
Python ‘os, shutil & glob’ 模块是文件处理活动的有效方式。以下是管理您正在使用的目录的一些基本方法:
import os
import shutil
import glob
#return current working directory(cwd)
cwd = os.getcwd()
#change cwd location
os.chdir('/Users/sammitiyadav/Desktop')
#add a folder in the cwd
os.mkdir('new folder')
#delete folder from the cwd
os.rmdir('new folder')
#create paths without having to use slashes
os.path.join(cwd, 'new folder')
#check if the directory exists
os.path.isdir(cwd) #returns true if it does and false otherwise
#change paths(location) of a file
#os.rename(old path, new path)
os.rename('test.xlsx', os.path.join(cwd, 'new folder', 'test.xlsx'))
shutil.move('test.xlsx', os.path.join(cwd, 'new folder', 'test.xls