创建新目录和移动文件时出现错误

2024-09-21 00:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,在创建一堆文件之后,将创建一个新目录,然后将这些文件移到新目录中。一开始还可以,但突然间我遇到了一个错误,我不知道为什么会这样。 编辑:我添加了我以前的代码(都在同一个模块中)给那些想知道的人。在

我的代码:

import os
import shutil
mydir = os.getcwd()

def edit_and_rename_files():
    f = filelist() # an earlier script that just makes a list of files I care about
    for item in f:
        with open(item, "r+") as id_file:
            for line in id_file:
               does something
              return my_dict # just a dict with ids I want to rename

        with open(item, "r+") as infile:
            content = infile.readlines()
            new_content = []
            for line in content: # going to copy and rewrite some lines
                new_line = line
                for original_id, new_id in my_dict.items():
                    new_line = new_line.replace(original_id, new_id + "\n")
                new_content.append(new_line)

       with open(item+"_renamed.fa", "w") as outfile:
           for line in new_content:
               outfile.write(line)

edit_and_rename_files()                 


def move_files():
    renamed_dir = mydir + "\\renamed"
    if not os.path.exists(renamed_dir):
        os.mkdir(renamed_dir)
    else:
        print "This directory already exists. Will move the files into this directory anyways."

   for root, dirs, files in os.walk(mydir):
        for fname in files:
            if fname.endswith("_renamed.fa"):
                shutil.move(fname, renamed_dir)

move_files()

这个脚本执行,文件被创建,实际上移动很好(当我打开文件时没有错误),但是我仍然得到一个错误,它让我很恼火。在

^{pr2}$

Tags: 文件inidnewformoveos错误

热门问题