Answers:
shutilඔබට භාවිතා කළ හැකි බොහෝ ක්රම තිබේ. ඉන් එකක් නම්:
from shutil import copyfile
copyfile(src, dst)
ඔබ භාවිතා කරන්නේ නම් os.pathමෙහෙයුම්, භාවිතා copyකරනවාට වඩා copyfile. copyfileඇත නූල් නිට්ෂේ .
~, නමුත් එයට සාපේක්ෂ මාර්ග සමඟ කටයුතු කළ හැකිය
┌──────────────────┬────────┬───────────┬───────┬────────────────┐
│ Function │ Copies │ Copies │Can use│ Destination │
│ │metadata│permissions│buffer │may be directory│
├──────────────────┼────────┼───────────┼───────┼────────────────┤
│shutil.copy │ No │ Yes │ No │ Yes │
│shutil.copyfile │ No │ No │ No │ No │
│shutil.copy2 │ Yes │ Yes │ No │ Yes │
│shutil.copyfileobj│ No │ No │ Yes │ No │
└──────────────────┴────────┴───────────┴───────┴────────────────┘
copy2(src,dst)බොහෝ විට වඩා ප්රයෝජනවත් copyfile(src,dst)වන්නේ:
dstදිය වී බහලුම (ඒ වෙනුවට සම්පූර්ණ ඉලක්කය ගොනු කිරීම), කුමන අවස්ථාවකදී basename ක srcනව ගොනු නිර්මාණය කිරීම සඳහා භාවිතා වේ;මෙන්න කෙටි උදාහරණයක්:
import shutil
shutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext') # complete target filename given
shutil.copy2('/src/file.ext', '/dst/dir') # target filename is /dst/dir/file.ext
copyfileවඩා සැලකිය යුතු වේගයකින්copy2
shutil.copy2('/dir/file.ext', '/new/dir/')(ඉලක්කගත මාර්ගයෙන් පසු කප්පාදුවක් සහිතව) “dir” නමින් නව ගොනුවකට පිටපත් කළ යුතුද නැතිනම් එම නාමයේ නාමාවලියකට ගොනුව දැමිය යුතුද යන්න පිළිබඳ අපැහැදිලි බව ඉවත් කරනු ඇතැයි මම සිතීම නිවැරදිද ?
/new/dirදැනට පවතින නාමාවලියක් නම් අවිනිශ්චිතතාවයක් නොමැත , බලන්න @ මැතිව් ඇල්පර්ට්ගේ අදහස.
/new/dir/නොපවතියි, Python විසිකිරීම ඇත IsADirectoryError, එසේ නොවන අවස්ථාවන්හීදී එය පිටපත් ලිපිෙගොනුව, /new/dir/මුල් නම යටතේ.
ඔබට shutilපැකේජයෙන් පිටපත් කිරීමේ කාර්යයන්ගෙන් එකක් භාවිතා කළ හැකිය :
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ක්රියාකාරී සංරක්ෂණ ආධාරක වෙනත් පිටපත් භාර ගනී
අවසර නාමාවලිය dest. ගොනුව obj පාර-දත්ත
-------------------------------------------------- ----------------------------
shutil.copy ✔ ✔ ☐ ☐
shutil.copy2 ✔ ✔ ☐ ✔
shutil.copyfile ☐ ☐ ☐
shutil.copyfileobj ☐ ☐
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
උදාහරණයක්:
import shutil
shutil.copy('/etc/hostname', '/var/tmp/testhostname')
පයිතන්හිදී, ඔබට ගොනු භාවිතයෙන් පිටපත් කළ හැකිය
shutil මොඩියුලයos මොඩියුලයsubprocess මොඩියුලයimport os
import shutil
import subprocess
shutilමොඩියුලය භාවිතයෙන් ගොනු පිටපත් කිරීමshutil.copyfile අත්සන
shutil.copyfile(src_file, dest_file, *, follow_symlinks=True)
# example
shutil.copyfile('source.txt', 'destination.txt')
shutil.copy අත්සන
shutil.copy(src_file, dest_file, *, follow_symlinks=True)
# example
shutil.copy('source.txt', 'destination.txt')
shutil.copy2 අත්සන
shutil.copy2(src_file, dest_file, *, follow_symlinks=True)
# example
shutil.copy2('source.txt', 'destination.txt')
shutil.copyfileobj අත්සන
shutil.copyfileobj(src_file_object, dest_file_object[, length])
# example
file_src = 'source.txt'
f_src = open(file_src, 'rb')
file_dest = 'destination.txt'
f_dest = open(file_dest, 'wb')
shutil.copyfileobj(f_src, f_dest)
osමොඩියුලය භාවිතයෙන් ගොනු පිටපත් කිරීමos.popen අත්සන
os.popen(cmd[, mode[, bufsize]])
# example
# In Unix/Linux
os.popen('cp source.txt destination.txt')
# In Windows
os.popen('copy source.txt destination.txt')
os.system අත්සන
os.system(command)
# In Linux/Unix
os.system('cp source.txt destination.txt')
# In Windows
os.system('copy source.txt destination.txt')
subprocessමොඩියුලය භාවිතයෙන් ගොනු පිටපත් කිරීමsubprocess.call අත්සන
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
# example (WARNING: setting `shell=True` might be a security-risk)
# In Linux/Unix
status = subprocess.call('cp source.txt destination.txt', shell=True)
# In Windows
status = subprocess.call('copy source.txt destination.txt', shell=True)
subprocess.check_output අත්සන
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
# example (WARNING: setting `shell=True` might be a security-risk)
# In Linux/Unix
status = subprocess.check_output('cp source.txt destination.txt', shell=True)
# In Windows
status = subprocess.check_output('copy source.txt destination.txt', shell=True)
['copy', sourcefile, destfile]හැකි සෑම තැනකම සින්ටැක්ස් භාවිතා කරන්න , විශේෂයෙන් පරාමිතීන් පරිශීලක ආදානයෙන් නම්.
os.popenදැන් ටික කලක් ඉවත් කර ඇත. සහ check_outputතත්වය ආපසු නොදෙන නමුත් ප්රතිදානය (එය හිස්ව පවතී copy/cp)
පහත උදාහරණ වලින් දැක්වෙන පරිදි ගොනුවක් පිටපත් කිරීම සාපේක්ෂව සරල මෙහෙයුමකි, නමුත් ඒ වෙනුවට ඔබ ඒ සඳහා shutil stdlib මොඩියුලය භාවිතා කළ යුතුය .
def copyfileobj_example(source, dest, buffer_size=1024*1024):
"""
Copy a file from source to dest. source and dest
must be file-like objects, i.e. any object with a read or
write method, like for example StringIO.
"""
while True:
copy_buffer = source.read(buffer_size)
if not copy_buffer:
break
dest.write(copy_buffer)
ඔබට ගොනු නාමයෙන් පිටපත් කිරීමට අවශ්ය නම් ඔබට මෙවැනි දෙයක් කළ හැකිය:
def copyfile_example(source, dest):
# Beware, this example does not handle any edge cases!
with open(source, 'rb') as src, open(dest, 'wb') as dst:
copyfileobj_example(src, dst)
shutil.copyfileobj. try, finallyව්යතිරේකයෙන් පසුව ලිපිගොනු වැසීම හැසිරවීමට ඔබට කිසිවක් නැත . කෙසේ වෙතත්, ලිපිගොනු විවෘත කිරීම සහ වැසීම සඳහා ඔබේ කාර්යය වගකිව යුතු නැති බව මම කියමි. එය කොතරම් මෙන් වූ දවටනය උත්සවයට යා යුතු shutil.copyfileආධාරක shutil.copyfileobj.
destලිවිය හැකි ලෙස සඳහන් කළ යුතුය:open(dest, 'wb')
ෂටිල් මොඩියුලය භාවිතා කරන්න .
copyfile(src, dst)
Src නම් ගොනුවේ අන්තර්ගතය dst නම් ගොනුවකට පිටපත් කරන්න. ගමනාන්ත ස්ථානය ලිවිය හැකි විය යුතුය; එසේ නොමැති නම්, IOError ව්යතිරේකයක් මතු කරනු ඇත. Dst දැනටමත් පවතී නම්, එය ප්රතිස්ථාපනය වේ. අක්ෂර හෝ බ්ලොක් උපාංග සහ පයිප්ප වැනි විශේෂ ලිපිගොනු මෙම ශ්රිතය සමඟ පිටපත් කළ නොහැක. src සහ dst යනු නූල් ලෙස ලබා දී ඇති මාර්ග නම් වේ.
දෙස බලන්න filesys සම්මත Python මොඩියුලයන් ලබා ගත කටයුතු භාරව සියලු ගොනු හා බහලුම් සඳහා.
නාමාවලිය සහ ගොනු පිටපත් උදාහරණය - ටිම් ගෝල්ඩන්ගේ පයිතන් බඩු වලින්:
http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html
import os
import shutil
import tempfile
filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2
shutil.copy (filename1, filename2)
if os.path.isfile (filename2): print "Success"
dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2
shutil.copytree (dirname1, dirname2)
if os.path.isdir (dirname2): print "Success"
පළමුවෙන්ම, මම ඔබේ යොමු කිරීම සඳහා ෂටල් ක්රම පිළිබඳ පරිපූර්ණ චෙට්ෂීට් එකක් සාදන ලදී.
shutil_methods =
{'copy':['shutil.copyfileobj',
'shutil.copyfile',
'shutil.copymode',
'shutil.copystat',
'shutil.copy',
'shutil.copy2',
'shutil.copytree',],
'move':['shutil.rmtree',
'shutil.move',],
'exception': ['exception shutil.SameFileError',
'exception shutil.Error'],
'others':['shutil.disk_usage',
'shutil.chown',
'shutil.which',
'shutil.ignore_patterns',]
}
දෙවනුව, පිටපත්වල පිටපත් කිරීමේ ක්රම පැහැදිලි කරන්න:
shutil.copyfileobj(fsrc, fdst[, length])විවෘත වස්තු හැසිරවීම
In [3]: src = '~/Documents/Head+First+SQL.pdf'
In [4]: dst = '~/desktop'
In [5]: shutil.copyfileobj(src, dst)
AttributeError: 'str' object has no attribute 'read'
#copy the file object
In [7]: with open(src, 'rb') as f1,open(os.path.join(dst,'test.pdf'), 'wb') as f2:
...: shutil.copyfileobj(f1, f2)
In [8]: os.stat(os.path.join(dst,'test.pdf'))
Out[8]: os.stat_result(st_mode=33188, st_ino=8598319475, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067347, st_mtime=1516067335, st_ctime=1516067345)
shutil.copyfile(src, dst, *, follow_symlinks=True)පිටපත් කර නැවත නම් කරන්න
In [9]: shutil.copyfile(src, dst)
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
shutil.copy()පාර-දත්ත පූර්වයෙන් තොරව පිටපත් කරන්න
In [10]: shutil.copy(src, dst)
Out[10]: ~/desktop/Head+First+SQL.pdf'
#check their metadata
In [25]: os.stat(src)
Out[25]: os.stat_result(st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066425, st_mtime=1493698739, st_ctime=1514871215)
In [26]: os.stat(os.path.join(dst, 'Head+First+SQL.pdf'))
Out[26]: os.stat_result(st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066427, st_mtime=1516066425, st_ctime=1516066425)
# st_atime,st_mtime,st_ctime changed
shutil.copy2()පාර-දත්ත පූර්වයෙන් පිටපත් කරන්න
In [30]: shutil.copy2(src, dst)
Out[30]: ~/desktop/Head+First+SQL.pdf'
In [31]: os.stat(src)
Out[31]: os.stat_result(st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067055, st_mtime=1493698739, st_ctime=1514871215)
In [32]: os.stat(os.path.join(dst, 'Head+First+SQL.pdf'))
Out[32]: os.stat_result(st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067063, st_mtime=1493698739, st_ctime=1516067055)
# Preseved st_mtime
shutil.copytree()
ගමනාන්ත නාමාවලිය නැවත ලබා දෙමින් src හි මුල් බැස ඇති සම්පූර්ණ නාමාවලි ගසක් පුනරාවර්තනය කරන්න
කුඩා ලිපිගොනු සඳහා සහ පයිතන් බිල්ට්-ඉන් පමණක් භාවිතා කිරීම සඳහා, ඔබට පහත එක් ලයිනර් භාවිතා කළ හැකිය:
with open(source, 'rb') as src, open(dest, 'wb') as dst: dst.write(src.read())
පහත දැක්වෙන අදහස් දැක්වීමේදී @maxschlepzig සඳහන් කර ඇති පරිදි, ගොනුව ඉතා විශාල හෝ මතකය තීරණාත්මක වන යෙදුම් සඳහා මෙය ප්රශස්ත ක්රමයක් නොවේ, එබැවින් ස්වාතිගේ පිළිතුර වඩාත් කැමති විය යුතුය.
.read()හා .write()පෙරනිමි (අවම වශයෙන් CPython සඳහා) විසින් buffered ඇත.
open()දීමෙන් පෙරනිමියෙන් ඔබට මෙහි උදව් නොකෙරේ, මන්ද යත්, read()'n negative ණ හෝ අතහැර දමා ඇත්නම්, ඊඕඑෆ් තෙක් කියවන්න.' ඒ කියන්නේ read()සම්පූර්ණ ගොනු අන්තර්ගතය නූලක් ලෙස ලබා දෙයි.
ඔබට භාවිතා කළ හැකිය os.system('cp nameoffilegeneratedbyprogram /otherdirectory/')
නැත්නම් මම ඒක කළා වගේ,
os.system('cp '+ rawfile + ' rawdata.dat')
rawfileවැඩසටහන තුළ මා ජනනය කළ නම කොහිද ?
මෙය ලිනක්ස් පමණක් විසඳුමකි
shutilලබා ගත නොහැකි විට පවා - subprocess.run() (නොමැතිව shell=True!) වඩා හොඳ විකල්පයයි os.system().
subprocess.run()@maxschlepzig විසින් යෝජනා කර ඇති පරිදි බාහිර වැඩසටහන් ඇමතීමේදී විශාල ඉදිරි පියවරකි. කෙසේ වෙතත් නම්යශීලීභාවය සහ ආරක්ෂාව ['cp', rawfile, 'rawdata.dat']සඳහා විධාන රේඛාව පසු කිරීමේ ස්වරූපය භාවිතා කරන්න . (කෙසේ වෙතත්, පිටපත් කිරීම සඳහා shutilසහ මිතුරන් බාහිර වැඩසටහනක් ඇමතීමට නිර්දේශ කරනු ලැබේ.)
විශාල ලිපිගොනු සඳහා, මා කළේ ගොනු රේඛාව රේඛාවෙන් කියවා එක් එක් පේළිය අරාවකට කියවීමයි. පසුව, අරාව යම් ප්රමාණයකට ළඟා වූ පසු, එය නව ගොනුවකට එකතු කරන්න.
for line in open("file.txt", "r"):
list.append(line)
if len(list) == 1000000:
output.writelines(list)
del list[:]
for l in open('file.txt','r'): output.write(l)සොයා ගත යුතු වැඩ; ඔබගේ අවශ්යතාවන්ට ප්රතිදාන ප්රවාහ බෆරය සකසන්න. හෝ වරකට ඔබ ලිවීමට කැමති බයිට් ගණන output.write(read(n)); output.flush()කොහේදැයි උත්සාහ කර බැලීමෙන් ඔබට බයිට් වලින් යා හැකිය n. මේ දෙකටම ප්රසාද දීමනාවක් දැයි පරීක්ෂා කිරීමට කොන්දේසියක් නොමැත.
shutil? නොසලකා හරින විට පවා shutil, සරල බ්ලොක් කියවීමේ / ලිවීමේ ලූපයක් (නොකැඩූ IO භාවිතා කිරීම) කෙළින්ම ඉදිරියට යනු ඇත, කාර්යක්ෂම වන අතර මෙයට වඩා බොහෝ අර්ථවත් වනු ඇත, එබැවින් ඉගැන්වීමට සහ තේරුම් ගැනීමට පහසුය.
from subprocess import call
call("cp -p <file> <file>", shell=True)
callඅනාරක්ෂිත ය. කරුණාකර ඒ පිළිබඳ උපප්රස්ථාර ලේඛනය වෙත යොමු වන්න.
වන විට පිඹුරා 3.5 ඔබ පහත සඳහන් කරන්න පුළුවන් කුඩා ගොනු (එනම්: පෙළ ගොනු, කුඩා jpegs) සඳහා:
from pathlib import Path
source = Path('../path/to/my/file.txt')
destination = Path('../path/where/i/want/to/store/it.txt')
destination.write_bytes(source.read_bytes())
write_bytes ගමනාන්තයේ පිහිටි ඕනෑම දෙයක් නැවත ලියයි
shutilඔබ වෙනුවෙන් සියලුම විශේෂ අවස්ථා හසුරුවන අතර ඔබට මනසේ සාමය ලැබේ.
open(destination, 'wb').write(open(source, 'rb').read())
ප්රභව ගොනුව කියවීමේ ප්රකාරයේදී විවෘත කර ගමනාන්ත ගොනුවට ලිවීමේ ක්රමයට ලියන්න.
.close()හැම දෙයක්ම නැතිවෙලා open(...)නේද?
මෙහෙයුම් පද්ධති ෂෙල් උපයෝගීතා භාවිතයෙන් ලිපිගොනු පහසුවෙන් පිටපත් කිරීම සඳහා පයිතන් තුළ ඇති කාර්යයන් සපයයි.
ගොනුව පිටපත් කිරීමට පහත විධානය භාවිතා කරයි
shutil.copy(src,dst)
මෙටා ඩේටා තොරතුරු සමඟ ගොනුව පිටපත් කිරීමට පහත විධානය භාවිතා කරයි
shutil.copystat(src,dst)
copyපසුව ධාවනය කළ යුතුය copystat. පයිතන් 3.3+ හි දීර් copystatextended කරන ලද ගුණාංග ද පිටපත් කරයි.