[英]How can I import xlsx file in to Jupiter notebook using directory?
[英]how can i rename using python any .xlsx file in a directory?
我在這里找不到類似的問題,但是如何使用目錄中的 python any.xlsx 文件重命名? 目標不是硬編碼文件名以將其重命名為其他名稱。 非常感謝任何意見或建議。 非常感謝你。
到目前為止我已經嘗試過什么。 它的作用是創建另一個 excel 文件,但我只需要將 C:\Test 中的 .xlsx 重命名為 Master.xlsx。
for root, dirs, files in os.walk("C:\Test", topdown=False,):
for name in files:
base_name, ext = os.path.splitext(name) #Split name, extension
if ext in ".xlsx":
df = pd.read_excel(os.path.join(root, name))
df.to_excel(os.path.join(root, 'Master.xlsx'), index=False)
您可以使用 glob 模塊找到 all.xlsx 文件,然后使用 os 模塊重命名它們。
獲取特定文件夾/目錄中的所有文件。
files = glob.glob(directory + '/*.xlsx')
然后遍歷它以獲取文件列表並使用 rename() 重命名這些文件。
os.rename(file, new_name)
完整代碼:
import glob
import os
files = glob.glob('./' + '/*.xlsx')
for file in files:
os.rename(file, file.replace('.xlsx', '_new.xlsx'))
使用操作系統客戶端 python
import os
os.chdir("C:\Test") #if you are running your code in 'c:\' location you need to do this so that your code will run inside test folder.
for file in os.listdir():
if file.endswith(".xlsx"):
os.rename(file, "new_name.xlsx")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.