2020-02-28 [转载] Python(pycharm)在windows下路径 ( ' / ' 与' \ ' )的问题

1.0 首先了解Python中与pycharm,windows交互的模块(这二个模块可以避免出现路径错误的问题)
**sys模块 **(侧重Python与pycharm交互)
提供的与路径相关的方法有:

__file__               # 当前执行文件所在的绝对路径
sys.path              # 当前执行文件下所有的路径 
sys.argv              # 当前执行文件所在的绝对路径,列表的形式['绝对路径']
sys.path.append(路径)           # 添加路径到当前的文件下

应用实例:一:
当start执行文件需要导入core文件夹下的模块时,路径的拼接,需要sys模块,
但是首先还要介绍os模块
**os模块 **(侧重Python与windows的交互)

os.path.abspath()     规范化路径(重要 可以将不确定'/'与'\'路径规范化)   # 规范的路径:E:/text1/练习与测试2/05text路径.py
os.path.dirname('E:/text1/练习与测试2/05text路径.py')             # 获取路径: E:/text1/练习与测试2
os.path.basename('E:/text1/练习与测试2/05text路径.py'))        # 获取路径: 05text路径.py
os.path.join(path1,path2)                                                           # 路径的拼接
os.path.exists(path)                                                                   # 判断文件是否存在

应用实例一:


Python(pycharm)在windows下路径 ( ' / ' 与' \ ' )的问题

注:要想在start文件中执行server中的func_server方法,就必须将core的路劲添加到start文件中,当start文件路径中存在 E:/text1/day32/demo6_file,才能通过导入core,找到server,引用其中的func_server方法;

import os
import sys

ori_path = __file__                                # E:/text1/day32/demo6_file/bin/start.py
path = os.path.dirname(ori_path)         # E:/text1/day32/demo6_file/bin
base_path = os.path.dirname(path)     # E:/text1/day32/demo6_file
sys.path.append(base_path)

from core import server
from core import client

server.func_server()

所有的这些操作都不会出现路径的问题但是当我们判断某个文件是否存在,或者要拼接文件的路径时,就可能会遇到路径出错的问题.
2.0 ' / '斜杠与' \ '反斜杠
Python在windows下的标准路径是:E:/text1/练习与测试2/05text路径.py 分割符是斜杠' / ' ,但是仍然可以识别 反斜杠' \ '
方法一: ' r ' 转义

import sys
import os

print(__file__)     # E:/text1/练习与测试2/05text路径.py
print(os.path.exists('E:/text1/练习与测试2/05text路径.py'))              # True
print(os.path.exists('E:\text1\练习与测试2\05text路径.py'))              # Flase
print(os.path.exists(r'E:\text1\练习与测试2\05text路径.py'))             # True

path=os.path.abspath(r'E:\text1\练习与测试2\05text路径.py')
print(path)                                                                                           # E:\text1\练习与测试2\05text路径.py
print(os.path.exists(path))                                                                   # True

方法二: 路径拼接时,os.path.abspath('') 规范路径

path=os.path.join(r'E:\text1\练习与测试2','05text路径.py')
new_path=os.path.abspath(path)
print(os.path.exists(new_path))

方法三: 添加扩展名(后缀名)

import sys
import os

head = {'filename': '1.EPIC**.mp4**',     # 加后缀名   (这不就是要知道文件的类型吗?)
        'filesize': None,
        'filepath': r'C:\Users\Administrator\Desktop\英语\Download\伦敦街头美食',
        'file_name': None}

file_name = os.path.join(head['filepath'], head['filename'])
print(os.path.exists(file_name))
file_name = os.path.abspath(file_name)
print(os.path.exists(file_name))

注:这三种方法要结合的用
附: 常见的文件后缀
(出处: https://jingyan.baidu.com/article/e3c78d64752db43c4c85f5fc.html?qq-pf-to=pcqq.group )

转自:https://blog.51cto.com/13747953/2315302

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容