zoukankan      html  css  js  c++  java
  • python操作文件

    OS模块

    1.getcwd()

    用来获取当前工作目录

    >>> import os
    >>> os.getcwd()
    'D:\Postgraduate\Python\python数据分析\python玩转数据'

    2.chdir(path)

    用来改变当前工作目录

    >>> diri = 'D:/Postgraduate/Python/python数据分析/python玩转数据'
    >>> os.chdir(diri)
    >>> os.getcwd()
    'D:\Postgraduate\Python\python数据分析\python玩转数据'

    3.listdir(path='.')

    用来列举当前目录下有哪些文件和子目录,默认值是'.',也可以用'..'代表上层目录

    >>> os.listdir('.')
    ['0.txt', '1.txt', 'hash.py', 'python基础1', 'python基础2', 'test_1.txt', 'test_2.txt', 'test_3.txt', 'test_4.txt', 'uwm-workshop', 'uwm-workshop.zip', '__pycache__', '强大的数据结构和python扩展库', '数据统计和可视化第五周', '数据统计和可视化第六周', '数据获取与表示', '文本相似度', '面向对象和图像用户界面', '项目实践']
    >>> os.listdir('..')
    ['python玩转数据', '嵩老师数据分析MOOC']
    >>> os.listdir(diri)
    ['0.txt', '1.txt', 'hash.py', 'python基础1', 'python基础2', 'test_1.txt', 'test_2.txt', 'test_3.txt', 'test_4.txt', 'uwm-workshop', 'uwm-workshop.zip', '__pycache__', '强大的数据结构和python扩展库', '数据统计和可视化第五周', '数据统计和可视化第六周', '数据获取与表示', '文本相似度', '面向对象和图像用户界面', '项目实践']

    os.path模块

    介绍常用的os.path.join(path1[,path2[,...]]),将路径名和文件名组合成一个完整的路径

    >>> diri = 'D:/Postgraduate/Python/python数据分析/python玩转数据'
    >>> newf = os.path.join(diri,'test_1.txt')
    >>> with open(newf,'r') as f:
        f.read()
    
        
    'In many applications it is useful to record not only the fingerprints of a document,but also the position of the fingerprints in the document. For example, we needpositional information to show the matching substrings in a user interface. An efficient implementation of winnowing also needs to retain the position of the most recently selected fingerprint. Figure 2(f) shows the set of [fingerprint, position] pairs for this example (the first position is numbered 0). To avoid the notational complexity of indexing all hashes with their position in the global sequence of hashes of k-grams of a document, we suppress most explicit references to the position of k-grams in documents in our presentation.'

    注意:文件打开以后记得关闭!

    人生苦短,何不用python
  • 相关阅读:
    IOS开发-点击View取消键盘输入
    IOS读写Plist文件最简单方法
    谷歌眼镜Mirror app开发之简单新闻浏览页面
    Shell下获取Android设备信息
    PhpStrom与PyCharm的激活码
    python之数据类型的转换(eval,json.dumps,json.loads)
    网络安全基础2--网络协议,服务,安全
    网络安全基础1
    第二条用例需要用到第一条用例返回结果中的某些值
    python更新字典下嵌套数组下嵌套字典的值
  • 原文地址:https://www.cnblogs.com/yqpy/p/8365093.html
Copyright © 2011-2022 走看看