本篇文章帶給大家的內容是關於Python文件操作的相關知識介紹(程式碼範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
1、檔案操作
1-1 遍歷資料夾與檔案
import os rootDir = "/path/to/root" for parent, dirnames, filenames in os.walk(rootDir): for dirname in dirnames: print("parent is:" + parent) print("dirname is:" + dirname) for filename in filenames: print("parent is:" + parent) print("filename is:" + filename) print("the full name of the file is:" + os.path.join(parent, filename))
1-2 取得檔案名稱與副檔名
import os path = "/root/to/filename.txt" name, ext = os.path.splitext(path) print(name, ext) print(os.path.dirname(path)) print(os.path.basename(path))
1-3 逐行讀取文字檔案內容
f = open("/path/to/file.txt") # The first method line = f.readline() while line: print(line) line = f.readline() f.close() # The second method for line in open("/path/to/file.txt"): print(line) # The third method lines = f.readlines() for line in lines: print(line)
1-4 寫入檔案
output = open("/path/to/file", "w") # output = open("/path/to/file", "w+") output.write(all_the_text) # output.writelines(list_of_text_strings)
1-5 判斷檔案是否存在
import os os.path.exists("/path/to/file") os.path.exists("/path/to/dir") # Only check file os.path.isfile("/path/to/file")
1-6 建立文件夾
import os # Make multilayer directorys os.makedirs("/path/to/dir") # Make single directory os.makedir("/path/to/dir")
以上是Python檔案操作的介紹(程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)