क्या पाइथन के साथ 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/*"))
कभी-कभी पाइथन में सबकुछ करना अच्छा होता है; फिर फिर, अक्सर कमांड को कॉल करने के लिए यह अच्छा लगता है कि आप जानते हैं कि काम कैसे नियंत्रित करें और जानें।
मैं इस अगर जब आवश्यकताओं को बदलने, लेकिन तब तक, यह कम और पठनीय है पुनर्लेखन करने में संकोच नहीं चाहते हैं - अधिक समय बेहतर बड़ी समस्याओं पर खर्च किया जाता है। वे कैसे बदल सकते हैं इसका एक अच्छा उदाहरण एक त्रुटि की रिपोर्ट कर रहा है: आपने इसके बारे में कुछ भी नहीं कहा है, लेकिन जब आवश्यक हो तो मैं सीपी के आउटपुट को पार्स नहीं करूँगा।
यह भी उल्लेखनीय नहीं है कि 'सीपी' आउटपुट उत्पन्न कर सकता है, जिसे आप नहीं चाहते हैं। Stdout/stderr को '/ dev/null' पर भेजने पर विचार करें। – bstpierre
यह अब मैं जो कर रहा हूं उसके समान ही है, लेकिन यह पोर्टेबल नहीं है –
@ विसो: क्या आप अपने लक्षित प्लेटफ़ॉर्म/वातावरण के साथ प्रश्न अपडेट कर सकते हैं? –
- 1. \ r से \ r \ n
- 2. । Python
- 3. Python
- 4. python
- 5. python
- 6. CRAN -R
- 7. मैं R
- 8. R स्ट्रिंग
- 9. Grep और Python
- 10. python-oauth2
- 11. (python) Scipy
- 12. Boost python
- 13. क्या Python
- 14. python क्लाइंट
- 15. मैं Python
- 16. i Python
- 17. Python cursor.execute
- 18. Django/Python
- 19. mysql-python
- 20. urlencode python
- 21. django/python
- 22. matplotlib/python
- 23. Django/Python
- 24. python-oauth2
- 25. सी ++ FileIO कॉपी-बनाम-प्रणाली ("cp file1.x file2.x)
- 26. Regex \ r पट्टी और \ n या \ r \ n
- 27. पायथन (या numpy) R
- 28. ggplot और R
- 29. "\ r" n "0 \ n"
- 30. विम: \ n बनाम \ r
यह वही नहीं है, मैं निर्देशिका की सामग्री की प्रतिलिपि बनाना चाहता हूं, निर्देशिका –