Python. IOError: [Errno 13] Permission denied: when i'm copying file

后端 未结 9 1322
轮回少年
轮回少年 2021-01-03 19:44

I have two folders: In, Out - it is not system folder on disk D: - Windows 7. Out contain \"myfile.txt\" I run the following command in python:

>>>          


        
相关标签:
9条回答
  • 2021-01-03 20:03

    This works for me:

    import os
    import shutil
    import random
    
    
    dir = r'E:/up/2000_img'
    output_dir = r'E:/train_test_split/out_dir'
    
    
    files = [file for file in os.listdir(dir) if os.path.isfile(os.path.join(dir, file))]
    
    if len(files) < 200:
        # for file in files:
        #     shutil.copyfile(os.path.join(dir, file), dst)
        pass
    else:
        # Amount of random files you'd like to select
        random_amount = 10
        for x in range(random_amount):
            if len(files) == 0:
                break
            else:
                file = random.choice(files)
                shutil.copyfile(os.path.join(dir, file), os.path.join(output_dir, file))
    
    0 讨论(0)
  • 2021-01-03 20:04

    Use shutil.copy2 instead of shutil.copyfile

    import shutil 
    shutil.copy2('/src/dir/file.ext','/dst/dir/newname.ext') # file copy to another file
    shutil.copy2('/src/file.ext', '/dst/dir') # file copy to diff directory
    
    0 讨论(0)
  • 2021-01-03 20:09

    use

    > from shutil import copyfile
    > 
    > copyfile(src, dst)
    

    for src and dst use:

    srcname = os.path.join(src, name)
    dstname = os.path.join(dst, name)
    
    0 讨论(0)
提交回复
热议问题