Python专题, 语言

使用Python产生必要的文件和删除特定文件名的文件

功能如标题所示。Python代码如下:

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/25943
"""


def main():
    directory = 'E:/test'
    creat_necessary_file(directory)
    # delete_file_with_specific_name(directory)

    # import guan
    # guan.creat_necessary_file(directory)
    # guan.delete_file_with_specific_name(directory)


def creat_necessary_file(directory, filename='readme', file_format='.md', content='', overwrite=None):
    import os
    directory_with_file = []
    missed_directory = []
    for root, dirs, files in os.walk(directory):
        for i0 in range(len(files)):
            if root not in directory_with_file:
                directory_with_file.append(root)
            if files[i0] == filename+file_format:
                if root not in missed_directory:
                    missed_directory.append(root)
    if overwrite == None:
        for root in missed_directory:
            directory_with_file.remove(root)
    for root in directory_with_file:
        os.chdir(root)
        f = open(filename+file_format, 'w', encoding="utf-8")
        f.write(content)
        f.close()
    

def delete_file_with_specific_name(directory, filename='readme', file_format='.md'):
      import os
      for root, dirs, files in os.walk(directory):
        for i0 in range(len(files)):
            if files[i0] == filename+file_format:
                os.remove(root+'/'+files[i0])


if __name__ == '__main__':
    main()
60 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

评论说明:
(1)在保留浏览器缓存的前提下,目前支持72小时自主修改或删除个人评论。如果自己无法修改或删除评论,可再次评论或联系我。如有发现广告留言,请勿点击链接,博主会不定期删除。
(2)评论支持Latex公式。把latexpage作为标签放在任何位置,评论中的公式可正常编译,示例:
$Latex formula$  [latexpage]

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注