为什么Python读取绝对路径要用正斜杠?
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入门的书中都有,而且最好的办法不是自己动手写几行代码试试么?
用斜杠多好啊\(^o^)/~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 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, 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.

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).

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.

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.

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).

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.
