惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
博客园 - 司徒正美
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
D
Docker
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
腾讯CDC
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
I
InfoQ
雷峰网
雷峰网
The Cloudflare Blog
美团技术团队
Engineering at Meta
Engineering at Meta

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Commonly-used Methods in Python OS Package
2016-03-17 · via jdhao's digital space

In this post I summarize the commonly-used methods in Python os package.

method related to paths#

methoddescription
os.path.joinjoin string to from a path
os.path.splitsplit directory and file name
os.path.basenameget the base name of file from a complete path
os.path.abspathget the absolute path a file
os.path.dirnameget the directory name from a path

If you want to combine several string to form a valid path, you can use os.path.join(). For example, if you want to combine "F:\\paper_ref", "cnn_classify", "hinton2012.bib" into a complete path,you can manually combine them using path separator of your system. But this way has some potential portability issue and is also error-prone. The best way is to use os.path.join method.

os.path.join("F:\\paper_ref","cnn_classify","hinton2012.bib")

os.path.split can split the directory name and file anme, for example,

os.path.split("d:/my_program/read_image.cc")
# output: ('d:/my_program', 'read_image.cc')

you can use os.path.basename to remove directory info and only get the base name of a file.

os.path.basename("f:/test/test.jpg") # output: 'test.jpg'

With os.path.abspath, we can get the absolute path of a file in a folder. Suppose there is file named colour_themes.conf under current directory, you can use following command to get its absolute path,

os.path.abspath('colour_themes.conf')
# output: '/home/jdhao/files/colour_themes.conf'

The os.path.dirname() method will return the path of a file (not including the file name)

os.path.dirname("file/music/song1.mp3")
# output: file/music

check path or file#

methoddecription
os.path.isabs()decide if a path is absolute path
os.path.isdir()decide if a path is directory path and check if it really exists
os.path.isfile()decide if the given string is a file, also check if it really exists
os.path.exists()check if a file or directory really exists

directory manipulations#

methoddescription
os.getcwd()get current working directory
os.chdir()change current working directory
os.listdir()list sub-directory and files under a certain directory1
os.mkdir()create a new directory
os.rmdir()remove an empty directory, it can not remove non-empty directory2
os.remove()delete a file

some attribute#

There are also several use attribute. For better portability of your code, you should considering using these attribute instead of hard code.

attributedescription
os.linesepline break character
os.sepseparator in a normal directory path name
os.pathsepseparator for the PATH environment variable

References#


  1. note that this method only output sub-directory and file names, with no path info ↩︎

  2. if you want to delete a non-empty directory, use shutil.rmtree ↩︎