Table of Contents
回复内容:
Home Backend Development Python Tutorial 为什么Python读取绝对路径要用正斜杠?

为什么Python读取绝对路径要用正斜杠?

Jun 06, 2016 pm 04:23 PM
python windows

windows目录都是反斜杆 ( \ )来分隔一个个子目录的
而到了 Python 或者 R 就需要使用正斜杆( / )来表示
这其中是否有什么原因?

回复内容:

因为 backslash 是邪路。你可能要说 Windows 比 Unix 进入 PC 还早,backslash 的渊源也和 Unix 差不多久,为什么 backslash 就是邪路了?因为你要是横就横到底,干脆连 Unix 的根基 C 语言也不要采用,否则你每次写 backslash 都要写成「\\」。 在windows读取文件可以用\,但在字符串里面\被作为转义字符使用,那么在描述路径时有两种方式

'c:\\a.txt',转义的方式
r'c:\a.txt',声明字符串不需要转义

这样就实现了用\来访问路径。


而Linux下路径是用/来分割,同样描述路径有两种方式,总是不舒服。


python提供了兼容处理,让Linux风格路径在windows下可用,于是很多时候大家都会选Linux风格。这样简单的脚本可以在两个平台下都能运行。

python对windows路径处理有一个库叫ntpath。

windows用\分割路径是因为当年DOS时代将/作为参数引导使用了,懒惰的某人不愿花时间处理相关解析,于是为了赶工粗暴的使用了\替代。

windows二者皆可,字符串前加r防止转意。比如r'c:/***/**'
linux只用'/'
如果你不确定,可以不用关心这种细节,用os.path.sep来组合路径,标准库os中提供各种分隔符(sep,linesep)等等用于跨平台。具体请看文档

多说几句,
这类io问题标准库的使用方法python入门的书中都有,而且最好的办法不是自己动手写几行代码试试么?

其实windows是正反都可以的,只是python挑了其中的一个 因为开源语言最初的目标操作系统一般是 Linux/Unix , Unix 用 / 做路径分隔 话说,Windows 主要用的不就是“反斜杠(Backslash)”吗? 因为正斜杠“/”被Win/dos用来做命令行参数分隔符了(比如“/?”)所以目录分隔符就只能采用反正常人习惯的反斜杠“\”了。要不然,程序对于命令行里“/abc”该解析为参数还是根目录下文件/目录无所适从。Unix在这里的选择好一些,用横线“-”前缀来表示命令行参数,比如“-h”,然后就理所当然的用符合正常人习惯的正斜杠“/”来分割目录了。 DOS里边的命令“dir /a” "format c: /q",DOS命令的参数是用的正斜杠"/",所以就没办法咯,win3.1和win95都是要先装DOS6.22才能用的啊,所以DOS里是用正斜杠"/",不是windows。 还要操心正反斜杠,移植性咋办?看起来python提供的跨平台feature算是白做了。 见过字符串里写八个反斜杠的╮(╯▽╰)╭

用斜杠多好啊\(^o^)/~
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Unable to access mysql from terminal Unable to access mysql from terminal Apr 08, 2025 pm 04:57 PM

Unable to access MySQL from the terminal may be due to: MySQL service not running; connection command error; insufficient permissions; firewall blocks connection; MySQL configuration file error.

HadiDB: A lightweight, horizontally scalable database in Python HadiDB: A lightweight, horizontally scalable database in Python Apr 08, 2025 pm 06:12 PM

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

How to use AWS Glue crawler with Amazon Athena How to use AWS Glue crawler with Amazon Athena Apr 09, 2025 pm 03:09 PM

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.

How to copy and paste mysql How to copy and paste mysql Apr 08, 2025 pm 07:18 PM

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

Can mysql database store images? Can mysql database store images? Apr 08, 2025 pm 05:27 PM

Storing images in a MySQL database is feasible, but not best practice. MySQL uses the BLOB type when storing images, but it can cause database volume swell, query speed and complex backups. A better solution is to store images on a file system and store only image paths in the database to optimize query performance and database volume.

See all articles