python开辟_python文件操纵

    添加时间:2013-7-29 点击量:

    关于python文件操纵的具体申明,大师可以参考:关于python的文件操纵


    官方API:os-Miscellaneous operating system interfaces


    下面是我做的demo:



     1 import re
    
    2 import os
    3 import time
    4
    5 #文件路径
    6 image_path = E:\\test\\20130627_140132Hongten.jpg
    7
    8 #文件夹路径
    9 dir_path = E:\\test\\hongten
    10
    11 #文件路径
    12 file_abs_path = E:\\test\\hongten.txt
    13
    14 #获得当前工作空间目次
    15 def getcwd():
    16 return os.getcwd()
    17
    18 #获取指定文件夹下面的所有文件及文件夹
    19 #若是指定的文件夹不存在,则返回响应的提示信息
    20 def listdir(dir_path):
    21 if os.path.exists(dir_path):
    22 return os.listdir(dir_path)
    23 else:
    24 return 目次+ dir_path + 不存在
    25
    26 def isfile(file_path):
    27 if os.path.exists(file_path):
    28 return os.path.isfile(file_path)
    29 else:
    30 return 文件+ dir_path + 不存在
    31
    32
    33 if __name__ == __main__:
    34 print(当前的工作空间是:{0}.format(getcwd()))
    35 print(当前的工作空间下的文件及目次:,listdir(getcwd()))
    36 print(# 40)
    37 print(listdir(c:\\test))
    38 print(# 40)
    39 print(isfile(image_path))
    40 print(# 40)
    41 array = os.path.split(image_path)
    42 print(array)
    43 #文件全名:20130627_140132Hongten.jpg
    44 file_full_name = array[1]
    45 name = os.path.splitext(file_full_name)
    46 #文件名:20130627_140132Hongten
    47 file_name = name[0]
    48 #文件后缀:.jpg
    49 file_ext = name[1]
    50 print(文件全名:{0},文件名:{1},文件后缀:{2}.format(file_full_name,file_name,file_ext))
    51 print(# 40)
    52 #创建空文件夹
    53 #os.mkdir(E:\\mydir)
    54 #创建多级目次
    55 #os.makedirs(rE:\\bb\\cc)
    56 print(# 40)
    57 #打开一个文件
    58 fp = open(file_abs_path,w+)
    59 #print(读取文件:{0}的第一行:{1}.format(file_abs_path,fp.readline()))
    60 #把文件每一行作为一个list的一个成员,并返回这个list。其实它的内部是经由过程轮回调用readline()来实现的。
    61 #若是供给size参数,size是默示读取内容的总长,也就是说可能只读到文件的一项目组。
    62 #print(读取文件:{0}所有内容:{1}.format(file_abs_path,fp.readlines()))
    63 content = this is a test message!!\ngood boy!\ngogo......\nhello,I\m Hongten\nwelcome to my space!
    64 fp.write(content)
    65 fp.flush()
    66 fp.close()
    67 fp = open(file_abs_path,r+)
    68 print(读取文件:{0}所有内容:{1}.format(file_abs_path,fp.readlines()))


    运行结果:



    Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
    
    Type
    copyright, credits or license() for more information.
    >>> ================================ RESTART ================================
    >>>
    当前的工作空间是:D:\Python33\workspace
    当前的工作空间下的文件及目次: [
    rename.py, test_annotation.py, test_class.py, test_exception.py, test_exit.py, test_file.py, test_getA.py, test_hello.py, test_import.py, test_input.py, test_loops.py, test_myclass.py, test_os.py, test_range.py, test_str.py, test_string.py, test_while.py, test_with.py]
    ########################################
    目次c:\test不存在
    ########################################
    True
    ########################################
    (E:\\test, 20130627_140132Hongten.jpg)
    文件全名:20130627_140132Hongten.jpg,文件名:20130627_140132Hongten,文件后缀:.jpg
    ########################################
    #
    #######################################
    读取文件:E:\test\hongten.txt所有内容:[this is a test message!!\n, good boy!\n, gogo......\n, hello,Im Hongten\n, welcome to my space!]
    >>>


    分享到: