使用python在目录中创建文件

teaLive

我正在编写一个python脚本,其中读取了一个文本文件,并且希望在另一个目录中创建一个新的文本文件。我写了以下代码:

def treatFiles(oldFile, newFile):   
    with open(oldFile) as old, open(newFile, 'w') as new:
        count = 0
        for line in old:
            count += 1
            if count%2 == 0:
                pass
            else:
                new.write(line)

if __name__ == '__main__':
    from sys import argv
    import os
    os.makedirs('NewFiles')
    new = '/NewFiles/' + argv[1]
    treatFiles(argv[1], new)

我尝试在与Python脚本相同的目录中的文本文件中运行此代码,但出现类似以下错误

FileNotFoundError: [Errno 2] No such file or directory: '/NewFiles/testFile'

显然,尚不清楚NewFiles应在其中创建新文件的目录...如何解决此问题?

丹妮卡

问题实际上是在UNIX中,这/NewFiles/意味着在根目录中名为的文件夹NewFiles,而不是当前目录中的文件夹拆下导线/,应该没问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章