有沒有簡單的方法來模擬使用python命令cp -r from_dir/* to_dir
? shutil.copytree
不適用,因爲to_dir
存在。cp -r from_dir/* to_dir使用python
回答
看一看的shutil.copytree
的源代碼,適應它,並使用:
def copytree(src, dst, symlinks=False, ignore=None):
"""Recursively copy a directory tree using copy2().
The destination directory must not already exist.
If exception(s) occur, an Error is raised with a list of reasons.
If the optional symlinks flag is true, symbolic links in the
source tree result in symbolic links in the destination tree; if
it is false, the contents of the files pointed to by symbolic
links are copied.
The optional ignore argument is a callable. If given, it
is called with the `src` parameter, which is the directory
being visited by copytree(), and `names` which is the list of
`src` contents, as returned by os.listdir():
callable(src, names) -> ignored_names
Since copytree() is called recursively, the callable will be
called once for each directory that is copied. It returns a
list of names relative to the `src` directory that should
not be copied.
XXX Consider this example code rather than the ultimate tool.
"""
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()
os.makedirs(dst)
errors = []
for name in names:
if name in ignored_names:
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks, ignore)
else:
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
try:
copystat(src, dst)
except OSError, why:
if WindowsError is not None and isinstance(why, WindowsError):
# Copying file access times may fail on Windows
pass
else:
errors.extend((src, dst, str(why)))
if errors:
raise Error, errors
你只需要copytree
使用正確的名稱(或相同名稱)
shutil.copytree("/path/from_dir","/destination/from_dir")
import glob
import subprocess
subprocess.check_call(["cp", "-rt", "to_dir"] + glob.glob("from_dir/*"))
有時候自己直接用Python做所有事情是很好的;那麼,再次調用你知道如何控制和知道作品的命令通常更好。
我會毫不猶豫地改寫這個如果當需求發生變化,但在此之前,它的短期和可讀 - 更多的時間更好地用更大的問題。他們如何改變的一個很好的例子是報告一個錯誤:你沒有提到這件事,但是一旦需要,我不會解析cp的輸出。
也值得一提的是'cp'可以產生輸出,你可能不想要。考慮將stdout/stderr發送到'/ dev/null'。 – bstpierre 2010-10-19 11:57:10
這與我現在正在做的事非常相似,但它不是便攜式的 – 2010-10-19 17:22:31
@wiso:您可以使用您的目標平臺/環境更新問題嗎? – 2010-10-19 17:27:10
- 1. PHP shell_exec cp -r
- 2. 無法用cp -R命令
- 3. 使用「CP」 Python的子進程錯誤
- 4. 爲什麼gsutil -m cp -R比gsutil cp -R快得多
- 5. cp使用虛線
- 6. 使用-cp
- 7. 在使用-cp
- 8. git的存檔VS CP -R
- 9. cp執行cp
- 10. cp -r與cp -recurse做同樣的事嗎?
- 11. 如何在python中執行linux中的「cp -r」
- 12. CP錯誤(合併使用與LS和grep) - CP:無法統計
- 13. 終端上的命令cp -r
- 14. Python - 編碼Kamenicky(CP-895)
- 15. 「cp -r」在bash中表現怪異嗎?
- 16. 沒有隱藏文件的cp -r
- 17. 在pl中執行cp -r sql
- 18. 使用AWK輸出的Linux CP
- 19. PowerShell命令,-r和-p是什麼意思? cp -r和cp -recurse之間的區別是什麼
- 20. Heroku運行cp -r試圖在錯誤的git remote上運行
- 21. cp複製wxPython空間
- 22. 使用cp -ur,但只更新目錄
- 23. 使用rpy2從Python調用R庫DirichletReg
- 24. 使用rpy2從python調用R庫「randomForest」
- 25. Ansible使用我有以下作用cp命令
- 26. Execution Java -cp
- 27. 批量添加額外\在cp路徑
- 28. 帶全路徑的Linux cp
- 29. 我如何在Mac OS中使用cp命令終端
- 30. 問題`cp -dR`
這是不一樣的,我想複製目錄的內容,而不是目錄 – 2010-10-20 08:32:53