Python [習題] 文件操作:目錄間copy 文件
阿新 • • 發佈:2017-10-26
權限 /tmp dir Coding clas 文件內容 文件 內容 實現
[習題] 指定一個源文件,實現copy到目標目錄。
例如把/tmp/sample1.txt 拷貝到/tmp/sample2.txt
原文件需要有讀權限(默認rt權限),目標文件需要給寫(w即可)權限。
In [8]: with open(‘/tmp/sample1.txt‘,encoding=‘UTF-8‘) as f1: ...: with open(‘/tmp/sample2.txt‘,‘w‘,encoding=‘utf-8‘) as f2: ...: content = f1.read() ...: f2.write(content) ...: In [9]: ls C:\python\171025 的目錄 2017/10/26 09:00 <DIR> . 2017/10/26 09:00 <DIR> .. 2017/10/25 19:20 13,099 sample1.txt 2017/10/26 09:01 13,099 sample2.txt
只拷貝了文件內容,文件元信息並未拷貝。
Python [習題] 文件操作:目錄間copy 文件