zoukankan      html  css  js  c++  java
  • python 复制文件流程

     例子代码:

    [root@master script]# vim copy_file.py    
    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    old_file_name = raw_input("Please input you need copy's file name:" )
    
    old_file = open(old_file_name,'r')
    
    new_file = open('langwang.txt','w')
    
    content = old_file.read()
    
    new_file.write(content)
    
    old_file.close()
    new_file.close()

    如果:hello.py  hello[复件].py 怎么实现呢?

    [root@master script]# vim copy_file.py
    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    old_file_name = raw_input("Please input you need copy's file name:" )
    
    old_file = open(old_file_name,'r')
    
    new_file_name = old_file_name.split('.')[0] + '[复件].' + old_file_name.split('.')[1]
    new_file = open(new_file_name,'w')
    
    content = old_file.read()
    
    new_file.write(content)
    
    old_file.close()
    new_file.close()

    还可以是:

    [root@master script]# vim copy_file.py    
    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    old_file_name = raw_input("Please input you need copy's file name:" )
    
    old_file = open(old_file_name,'r')
    
    #new_file_name = old_file_name.split('.')[0] + '[复件].' + old_file_name.split('.')[1]
    position = old_file_name.rfind(".")
    new_file_name = old_file_name[:position] + '[复件]' + old_file_name[position:]
    new_file = open(new_file_name,'w')
    
    content = old_file.read()
    
    new_file.write(content)
    
    old_file.close()
    new_file.close()

    提示:

    当一个文件非常大的时候,禁止使用read()读取,内存空间有限,用read()读取大文件会出现memory err ,内存不足的情况

    这种情况就没有解决办法吗?有的====》推荐使用如下代码:

    [root@master script]# cat copy_file.py 
    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    old_file_name = raw_input("Please input you need copy's file name:" )
    
    old_file = open(old_file_name,'r')
    
    position = old_file_name.rfind(".")
    new_file_name = old_file_name[:position] + '[复件]' + old_file_name[position:]
    
    new_file = open(new_file_name,'w')
    while True:
        content = old_file.read(1024)
        if not content:
            break
        new_file.write(content)
    
    old_file.close()
    new_file.close()

     ##########文件的位置############

    In [17]: f = open("hello.py",'r')
    
    In [18]: f.read()
    Out[18]: "#!/usr/bin/python
    #-*- coding:utf-8 -*-
    
    i = 1
    
    while i<=5:
        j = 1
        while j<=i:
            print '*',
            j+=1
        print ''
        i+=1
    
    "
    
    In [19]: f.seek(0,0)        ##指针,seek(0,0) 代表从头开始读
    
    In [20]: f.read()
    Out[20]: "#!/usr/bin/python
    #-*- coding:utf-8 -*-
    
    i = 1
    
    while i<=5:
        j = 1
        while j<=i:
            print '*',
            j+=1
        print ''
        i+=1
    
    "
    
    In [21]: f.read()
    Out[21]: ''
    
    In [22]: f.tell()    ##指针的位置索引
    Out[22]: 141
    
    In [23]: f.read()
    Out[23]: ''
    
    In [24]: f.tell()
    Out[24]: 141
    
    In [25]: f.seek(0,0)
    
    In [26]: f.tell()
    Out[26]: 0
    

      

  • 相关阅读:
    打理自己的生活
    多线程练习 -- 自定义NSOperation
    多线程练习 -- 单例设计模式
    IOS学习笔记 -- 多线程
    画画板 -- 可自定义线的宽度和颜色
    手势识别器基本练习
    触摸事件练习 -- 手势解锁
    触摸事件练习 -- 画画板(截屏分类)
    Main.storyboard
    Quartz2D练习 -- 裁剪图片分类
  • 原文地址:https://www.cnblogs.com/shanhua-fu/p/7641113.html
Copyright © 2011-2022 走看看