【Python进阶(五)】——模块搜索及工作目录

简介: 【Python进阶(五)】——模块搜索及工作目录

【Python进阶(五)】——模块搜索及工作目录,建议收藏!

该篇文章利用Python展示了模块搜索和工作目录的相关操作。

1 搜索路径

1.1 变量搜索路径

  运行程序:

dir()

  运行结果:

['In',
 'NamespaceMagics',
 'Out',
 '_',
 '_Jupyter',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_getshapeof',
 '_getsizeof',
 '_i',
 '_i1',
 '_i2',
 '_i3',
 '_i4',
 '_i5',
 '_i6',
 '_i7',
 '_i8',
 '_i9',
 '_ih',
 '_ii',
 '_iii',
 '_nms',
 '_oh',
 'exit',
 'get_ipython',
 'getsizeof',
 'json',
 'myfunc1',
 'np',
 'quit',
 'var_dic_list']

1.2 变量重定义

运行程序:

vi=1#将变量放入搜索路径的方法:用赋值语句定义一个新变量
dir()

1.3 变量移除

运行程序:

del vi #移除变量
vi

1.4 模块搜索路径

运行程序:

import sys
sys.path  #sys模块中提供的属性path

1.5 添加模块搜索路径

运行程序:

import sys
sys.path.append('H:\\Python\\Anaconda')#增加新路径到模块搜索路径
sys.path

1.6 移除模块搜索路径

sys.path.remove('H:\\Python\\Anaconda')#移除模块中路径
sys.path

2 工作目录

2.1 显示当前工作目录的方法

  运行程序:

import os
print(os.getcwd())

  运行结果:

D:\pythonjupyter

2.2 更改当前工作目录的方法

  运行程序:

os.chdir('D:\pythonjupyter')
print(os.getcwd())

  运行结果:

D:\pythonjupyter

2.3 读、写当前工作目录的方法

  运行程序:

from pandas import read_csv
data = read_csv('bc_data.csv') #读取csv文件


相关文章
|
2天前
|
安全 项目管理 Python
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
|
2天前
|
Python
像导入Python模块一样导入ipynb文件
像导入Python模块一样导入ipynb文件
|
2天前
|
Python
如何在 Python 中导入模块
【8月更文挑战第29天】
13 1
|
2天前
|
Python
|
2天前
|
数据采集 JSON 算法框架/工具
我常用的几个经典Python模块
我常用的几个经典Python模块
|
2天前
|
开发者 Python
什么是 python 模块?
【8月更文挑战第29天】
4 0
Python进阶系列(十八)
Python进阶系列(十八)
|
测试技术 Python
Python进阶系列(十七)
Python进阶系列(十七)
|
存储 缓存 Python
Python进阶系列(十六)
Python进阶系列(十六)
|
Python Windows
Python进阶系列(十五)
Python进阶系列(十五)
下一篇
云函数