Errno 22无效参数Python

时间:2019-05-27 14:21:16

标签: python file shutil file-copying

我正在尝试将一个文件的内容复制到另一个文件,并且出现错误,我该怎么办?

for file in os.listdir('offer'):
    if '.css' in file:
        print(file)
        with open(f'offer/{file}', 'r+') as f:
            with open('offer/id.css', 'w+') as style_file:
                shutil.copyfile(f'offer/{f}', f'offer/{style_file}')
Traceback (most recent call last):
  File "C:/Users/Katerina/Desktop/python/test_attempt.py", line 50, in <module>
    shutil.copyfile(f'offer/{f}', f'offer/{style_file}')
  File "C:\Users\Katerina\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
OSError: [Errno 22] Invalid argument: "offer/<_io.TextIOWrapper name='offer/id.css' mode='r+' encoding='cp1251'>"

2 个答案:

答案 0 :(得分:1)

我想您正在寻找类似的东西

with open('offer/id.css', 'w') as style_file:
    for file in os.listdir('offer'):
        if '.css' in file:
            #print(file)
            with open(f'offer/{file}', 'r+') as f:
                for line in f:
                    style_file.write(line)

shutil.copyfile的目的有所不同;它实际上并不允许您访问或修改两个文件的内容,而只是将一个文件复制到另一个文件。

答案 1 :(得分:1)

我知道这不是您的问题,但是当我在运行[Errno 22] Invalid Argument时遇到一般的shutil.copy错误时,问题是该应用程序帐户的目标文件夹的权限已更改。我只需要将文件夹的Write Permissions还原到运行该应用程序的帐户即可。