温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-11 05:19:56 来源:网络 阅读:7755 作者:even_07 栏目:编程语言

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(path2,path3)                                                           # 路径的拼接
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 )
.AVI .mp4影像文件
.BAK 备份文件
.BAT DOS中的批处理文件
.BMP 位图文件
.COM, .EXE 可执行的程序文件
.DLL 动态链接库文件
.DOC Word字处理文档
.DRV 设备驱动程序文件
.ICO 图标文件
.INF 安装信息文件
.INI 系统配置文件
.CHM 已编译的HTML帮助文件
.JPG 一种常用图形文件
.MID MIDI音乐文件
.MDB Access数据库文件
.RTF 丰富文本格式文件
.SCR 屏幕保护程序文件
.SYS 系统文件
.TTF TrueType字体文件
.TXT 文本文件
.XLS Excel电子表格文件
.WAV 波形文件
.HTM 用于WWW(World Wide Web)的超级文本文件

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI