将 .txt 文件保存在 Python 的另一个文件夹中
Saving the .txt files in another folder in Python
有一些轻量级的文本搜索引擎。它遍历一个文件夹中的 .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)
但它似乎它不起作用。这样做的正确方法是什么?
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'
为writing[=38]的模式=]. 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_text
and write_text
方法等。
有一些轻量级的文本搜索引擎。它遍历一个文件夹中的 .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)
但它似乎它不起作用。这样做的正确方法是什么?
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'
为writing[=38]的模式=]. 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_text
and write_text
方法等。