将.txt文件保存在Python的另一个文件夹中

时间:2018-07-05 12:58:51

标签: python file python-os

拥有一些轻量级的文本搜索引擎。遍历一个文件夹中的.txt文件,搜索所选术语并显示文件名。全部基于os库的功能:

import os

dirname = '/Users/user/Desktop/test/reports'
save_path = '/Users/user/Desktop/test/folder'

search_terms = ['card']
search_terms = [x.lower() for x in search_terms]
Word = ''.join(search_terms)

for f in os.listdir(dirname):
    with open(os.path.join(dirname,f), "r", encoding="latin-1") as infile:
        text =  infile.read()

    if all(term in text for term in search_terms):
        print (f)
        os.path.join(save_path,f)  

3001017.txt
3003402.txt
3002866.txt
3003763.txt
3004961.txt
3003834.txt
3001986.txt

搜索本身运行良好,但我想作为最后一项操作将结果中接收到的.txt文件保存在另一个文件夹save_path中,为此我尝试使用os.path.join(save_path,f),但似乎它不起作用。正确的做法是什么?

1 个答案:

答案 0 :(得分:2)

os.path.join合并字符串看起来像一个路径,并且与将数据保存到文件无关。

请参考此示例,其中uses the pathlib module

In [1]: from pathlib import Path

In [3]: p = Path.cwd()

In [4]: p
Out[4]: PosixPath('/home/bluesmonk')

In [7]: p.stem
Out[7]: 'bluesmonk'

In [9]: p.is_dir()
Out[9]: True

In [10]: p2 = p.joinpath('foo') # path does not exist

In [11]: p2.is_dir()
Out[11]: False

请注意,创建p2不会在文件系统中创建任何内容。

关于如何保存文件,您需要指定 mode 作为第二个参数,即'w'用于写入的模式。 Check the documentation for more info.

In [12]: with open(p.joinpath('foo.txt'), 'w') as saved_file:
    ...:     txt = 'hello world!'
             print(saved_file)
    ...:     saved_file.write(txt)
    ...:     
<_io.TextIOWrapper name='/home/bluesmonk/foo.txt' mode='w' encoding='UTF-8'>


In [13]: ls
 Code/       Documents/         Library/    Public/      Vagrant/
 Desktop/    Downloads/         Music/      snap/        Videos/
 foo.txt     examples.desktop   Pictures/   Templates/  'VirtualBox VMs'/

In [14]: cat foo.txt
hello world!

您的代码因此看起来像

for f in os.listdir(dirname):
    with open(os.path.join(dirname,f), "r", encoding="latin-1") as infile:
        text =  infile.read()

    if all(term in text for term in search_terms):
        print (f)
        with open(os.path.join(save_path,f), 'w') as sf:
            sf.write(text) 

还请注意,pathlib公开了read_textwrite_text方法,等等。