三分钟热情自学 Python · 第11期 · 使用 Python 处理文件路径

博观而约取,厚积而薄发。
——苏轼

1. 前言

在这个生活中处处都是大数据和人工智能的时代,总是能在各种角落看到 Python 的培训广告。我招架不住敌方的猛烈攻势,败下阵来。经过了一分钟的深思熟虑,我决定利用我的三分钟热情进行回击,从零开始自学 Python

2.

File Paths

文件路径

File paths in different operating system:
不同操作系统中的文件路径:

  • Windows:
    Use the backslashes (\) as the path separator.
    使用反斜杠 \ 作为路径分隔符。
    C:\Users\python

  • macOS & Linux:
    Use the forward slash (/) as the path separator.
    使用正斜杠 / 作为路径分隔符。
    /home/python/

2.1

Use pathlib to Deal with File Paths

使用 pathlib 处理文件路径

Always use forward slashes with pathlib functions is a good idea. The Path() object will convert forward slashes into the correct kind of slash for the current operating system.

在使用 pathlib 模块的函数时,最好一直都采用正斜杠作为分隔符。Path() 对象会将正斜杠转换为当前操作系统所对应的斜杠类型。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
支付 ¥2.88 继续阅读