在python中實現對檔案的寫入,讀取,複製,批量重新命名

來源:互聯網
上載者:User

標籤:python   檔案操作   

1.寫入內容至檔案中

def write_file():    open_file = open("xxxx.txt","w")    open_file.write("i want to open a file and write this.\n")    open_file.close()write_file()

2.讀取檔案中的內容

#思路:1.以什麼方式開啟 2.讀取檔案 3.關閉檔案def read_file():    read_file = open("xxxx.txt","r")    content = read_file.read()    print(content)    read_file.close()read_file()

3.複製檔案

‘‘‘思路:    1.擷取使用者要複製的檔案名稱    2.執行複製        2.1開啟要複製的檔案名稱        2.2建立一個檔案        2.3從舊檔案中讀取資料,並且寫入到新檔案中    3.關閉檔案‘‘‘def copy_file():    #1.擷取使用者要複製的檔案名稱    old_file_name = input("請輸入要複製的檔案名稱:")    #2.1開啟要複製的檔案名稱    old_file = open(old_file_name,"r")    #2.2建立一個檔案名稱    position = old_file_name.rfind(".")    new_file_name = old_file_name[:position] + "[複件]" + old_file_name[position:]    new_file = open(new_file_name,"w")    #2.3從舊檔案中讀取資料,並且寫入到新檔案中    while True:        content = old_file.read(1024)        if len(content) == 0:            break        new_file.write(content)    #關閉檔案    old_file.close()    new_file.close()copy_file()

4.批量重新命名
1)環境準備

    mkdir /appdata/test/    cd /appdata/test/;touch {1..10}.txt;ll    [[email protected] test]# cd /appdata/test/;touch {1..10}.txt;lltotal 0-rw-r--r-- 1 root root 0 May  6 15:59 10.txt-rw-r--r-- 1 root root 0 May  6 15:59 1.txt-rw-r--r-- 1 root root 0 May  6 15:59 2.txt-rw-r--r-- 1 root root 0 May  6 15:59 3.txt-rw-r--r-- 1 root root 0 May  6 15:59 4.txt-rw-r--r-- 1 root root 0 May  6 15:59 5.txt-rw-r--r-- 1 root root 0 May  6 15:59 6.txt-rw-r--r-- 1 root root 0 May  6 15:59 7.txt-rw-r--r-- 1 root root 0 May  6 15:59 8.txt-rw-r--r-- 1 root root 0 May  6 15:59 9.txt

2)程式部分

‘‘‘思路:    1.擷取要重新命名的檔案的檔案夾路徑    2.擷取所有要重新命名的檔案    3.執行重新命名‘‘‘import osdef rename_batch():    #1.擷取檔案夾    folder_name = input("請輸入要重新命名檔案的檔案夾路徑:")    #2.擷取所有檔案名稱    file_names = os.listdir(folder_name)    #3.重新命名    for name in file_names:        print(name)        old_file_name = folder_name + "/" + name        new_file_name = folder_name + "/" + "huwho出品-" + name        os.rename(old_file_name,new_file_name)         print(new_file_name)rename_batch()

3)執行結果部分

python rename_batch.py 請輸入要重新命名檔案的檔案夾路徑:/appdata/test1.txt/appdata/test/huwho出品-1.txt2.txt/appdata/test/huwho出品-2.txt3.txt/appdata/test/huwho出品-3.txt4.txt/appdata/test/huwho出品-4.txt5.txt/appdata/test/huwho出品-5.txt6.txt/appdata/test/huwho出品-6.txt7.txt/appdata/test/huwho出品-7.txt8.txt/appdata/test/huwho出品-8.txt9.txt/appdata/test/huwho出品-9.txt10.txt/appdata/test/huwho出品-10.txt

在python中實現對檔案的寫入,讀取,複製,批量重新命名

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.