将文件夹中的所有文件存储在目录中后,如何读取它们以便我可以对它们执行某些操作?

问题描述 投票:0回答:1

我可以使用

os.listdir
打开文件夹中的所有文件,但之后如何读取每个文件,然后在它们上执行一些代码?

encode  = 'ANSI'
file_paths = os.listdir('C:\\Users\\TMPFQB\\Documents\\MY PROJECTS\\Jan 2024')
for file_path in range(len(file_paths)):
    with open(file_path, 'r', encoding = encode) as f:
        Jan_txt = f.readlines()[4:]

使用此代码,我收到以下错误消息:

ValueError                                Traceback (most recent call last)
Cell In[14], line 6
      4 file_paths = os.listdir('C:\\Users\\TMPFQB\\Documents\\MY PROJECTS\\Jan 2024')
      5 for file_path in range(len(file_paths)):
----> 6     with open(file_path, 'r', encoding = encode) as f:
      7         Jan_txt = f.readlines()[4:]

File ~\AppData\Local\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py:280, in _modified_open(file, *args, **kwargs)
    277 @functools.wraps(io_open)
    278 def _modified_open(file, *args, **kwargs):
    279     if file in {0, 1, 2}:
--> 280         raise ValueError(
    281             f"IPython won't let you open fd={file} by default "
    282             "as it is likely to crash IPython. If you know what you are doing, "
    283             "you can use builtins' open."
    284         )
    286     return io_open(file, *args, **kwargs)

ValueError: IPython won't let you open fd=0 by default as it is likely to crash IPython. If you know what you are doing, you can use builtins' open.

下面显示了我打算如何将整个文本组合成一个字符串,然后使用 reGex 提取我需要的内容: (但我一直试图在文件夹中的每个文件上执行此操作)

whole_txt = "".join(Jan_16_txt)
matches = re.findall(pattern, whole_txt, flags = re.MULTILINE)
Jan_16 = [[m[8], m[12], m[27], m[29]] for m in matches]
df = pd.DataFrame(data = Jan_16)
python python-3.x pandas valueerror
1个回答
0
投票

正如 @JKupzig 在评论中已经建议和解释的那样,问题在于您的迭代,而不是引用要打开的相应文件的路径。

base_path = r'C:\Users\TMPFQB\Documents\MY PROJECTS\Jan 2024'

# List of files in above directory, making sure no directories are included
file_names = [file_name for file_name in os.listdir(base_path) if os.isfile(os.path.join(base_path, file_name)]

# You can also do this in above list comprehension directly, but to make it easier to understand...
# Create absolute paths to these files
file_paths = [os.path.join(base_path, file_name) for file_name in file_names]

for file_path in file_paths:
    with open(file_path, 'r', encoding = encode) as f:
        .... # your logic
© www.soinside.com 2019 - 2024. All rights reserved.