os 模块 python file 与文件路径
2014-12-10 19:05
555 查看
eg1:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env python
print __file__
[root@kooxoo20-180 sersync]# python test.py
test.py ##me相对路径得到的是相对路径
[root@kooxoo20-180 sersync]# python /home/wuxy/sersync/test.py
/home/wuxy/sersync/test.py ##绝对路径得到的是绝对路径
eg2:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env python
import os
a=os.path.realpath(__file__)
print a
[root@kooxoo20-180 sersync]# python test.py
/home/wuxy/sersync/test.py
[root@kooxoo20-180 sersync]# python /home/wuxy/sersync/test.py
/home/wuxy/sersync/test.py ##不管怎么执行,得到的都是绝对路径
eg3:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env python
import os
a=os.path.realpath(__file__)
print a
FILE_PATH=os.path.dirname(a)
print FILE_PATH
FILE_PATH=os.path.dirname(os.path.realpath(__file__)) ##建议使用这种方式
print FILE_PATH
[root@kooxoo20-180 sersync]# python test.py
/home/wuxy/sersync/test.py
/home/wuxy/sersync
/home/wuxy/sersync ##调用变量和不使用变量的print值都是一样的
eg4:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env python
import os
FILE_PATH=os.path.dirname(os.path.realpath(__file__))
PYCORE_PATH = os.path.realpath(os.path.join(FILE_PATH, '..', 'pycore'))
print PYCORE_PATH
[root@kooxoo20-180 sersync]# pwd
/home/wuxy/sersync
[root@kooxoo20-180 sersync]# python test.py
/home/wuxy/pycore
##me:os.path.join 用 '/' 将各个路径连接起来。
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env python
print __file__
[root@kooxoo20-180 sersync]# python test.py
test.py ##me相对路径得到的是相对路径
[root@kooxoo20-180 sersync]# python /home/wuxy/sersync/test.py
/home/wuxy/sersync/test.py ##绝对路径得到的是绝对路径
eg2:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env python
import os
a=os.path.realpath(__file__)
print a
[root@kooxoo20-180 sersync]# python test.py
/home/wuxy/sersync/test.py
[root@kooxoo20-180 sersync]# python /home/wuxy/sersync/test.py
/home/wuxy/sersync/test.py ##不管怎么执行,得到的都是绝对路径
eg3:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env python
import os
a=os.path.realpath(__file__)
print a
FILE_PATH=os.path.dirname(a)
print FILE_PATH
FILE_PATH=os.path.dirname(os.path.realpath(__file__)) ##建议使用这种方式
print FILE_PATH
[root@kooxoo20-180 sersync]# python test.py
/home/wuxy/sersync/test.py
/home/wuxy/sersync
/home/wuxy/sersync ##调用变量和不使用变量的print值都是一样的
eg4:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env python
import os
FILE_PATH=os.path.dirname(os.path.realpath(__file__))
PYCORE_PATH = os.path.realpath(os.path.join(FILE_PATH, '..', 'pycore'))
print PYCORE_PATH
[root@kooxoo20-180 sersync]# pwd
/home/wuxy/sersync
[root@kooxoo20-180 sersync]# python test.py
/home/wuxy/pycore
##me:os.path.join 用 '/' 将各个路径连接起来。
相关文章推荐
- Python 学习File、os模块操作文件
- python中利用os模块打印任意文件夹里面的文件名字,当前文件夹路径,已经其的目录名字
- Python Os模块--路径、文件、系统命令等操作
- python3 常用模块(时间、os路径及文件操作、字符串比较,文件内容比较等模块)
- Python os模块--路径、文件、系统命令等操作
- Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)
- Python使用os模块和fileinput模块来操作文件目录
- python中OS模块获得文件路径的流程
- python,os操作文件,文件路径(上一级目录)
- Python OS 文件操作模块常用函数
- Python标准库03 路径与文件 (os.path包, glob包)
- python文件读写,python文件os模块
- Python标准库03 路径与文件 (os.path包, glob包)
- python os模块,平常操作文件方便,执行cmd命令
- Python模块学习 ---- zipfile zip文件操作
- Python学习笔记10:标准库之路径和文件(os.path包,glob包)
- python用zipfile模块打包文件或是目录、解压zip文件实例
- 使用fileinput模块进行原地修改文件 分类: python 小练习 2013-08-13 16:47 618人阅读 评论(0) 收藏
- Python文件替代fileinput模块
- # 输出目录下的文件 os模块 分类: python 小练习 2013-05-13 18:01 205人阅读 评论(0) 收藏