0

运行以下代码后,

import os, shutil, glob, sys
source_dir = sys.argv[1]

list = ['file1', 'fil2']
for seek in list:
        if seek == 'file1':
                dest_dir = '/root/my_dir'
        elif seek == 'file2':
                dest_dir = '/root/my_dir'
        paths = [(z, os.path.join(dest_dir, os.path.split(z)[1]))
                for z in glob.glob(os.path.join(source_dir, seek))]
        for p in paths:
                if os.path.isfile(p[1]):
                        new_path = p[1] + '.bak'
                        print ('File "{0}" already exists; 
                                moving to "{1}"'.format(p[1], new_path))
                        shutil.move(p[1], new_path)
                shutil.copy2(p[0], p[1])

产生以下异常:

File "./config1.py", line 22, in <module>
  shutil.copy2(p[0], p[1])
File "/usr/local/lib/python3.0/shutil.py", line 99, in copy2
  copyfile(src, dst)
File "/usr/local/lib/python3.0/shutil.py", line 54, in copyfile
  copyfileobj(fsrc, fdst)
File "/usr/local/lib/python3.0/shutil.py", line 30, in copyfileobj
  fdst.write(buf)
File "/usr/local/lib/python3.0/io.py", line 1055, in write
  self._flush_unlocked()
File "/usr/local/lib/python3.0/io.py", line 1082, in _flush_unlocked
    n = self.raw.write(self._write_buf)
IOError: [Errno 28] No space left on device
4

1 回答 1

2

你的驱动器满了吗?

此外,那些行

    if seek == 'file1':
            dest_dir = '/root/my_dir'
    elif seek == 'file2':
            dest_dir = '/root/my_dir'

没用,无论哪种情况,您都将 dest_dir 设置为相同的值。

做就是了

dest_dir = '/root/my_dir'
list = ['file1', 'fil2']
for seek in list:
    # your stuff

或者,如果您打算处理其他不同的案例,

for seek in list:
    if seek in ('file1', 'fil2'):
        dest_dir = '/root/my_dir'
    elif seek in ('other_case', 'another_case'):
        dest_dir = '/foo'
于 2013-07-16T08:11:07.443 回答