写代码以迭代目录中的所有文件,并将每个文件的词典路径附加

发布于 2025-01-25 08:50:15 字数 2015 浏览 2 评论 0原文

我需要在Python模块中实现以下内容。

  1. 通过ARGPARSE模块使用该目录作为参数,
  2. 该目录在该目录中的所有文件中迭代目录。
  3. 后来,仅将CSV文件附加到字典中并将其存储在此处。

我尝试了操作系统,但它没有返回任何东西。

我已经编写了代码以获取目录作为输入,然后迭代文件以获取文件。但这会引发错误。

以下是代码

 import argparse
 import os
 import re
    
 # Accepting arguments for source_directory, my_sql connection details and table name to 
   update the MySQL entries to
    
parser = argparse.ArgumentParser(
        description='Utility to update MySQL tables on remote host')
parser.add_argument('-s', '--source_dir', help='Enter Path For CSV File')
    
args = parser.parse_args()
    
# Empty dictionary to store list CSV files in directory
files_in_dir = {}
    
# Function to iterate through the source_directory and read-only CSV files and append them 
   to files_in_dir dictionary
    
if args.source_dir:
    for file in args.source_dir:
    
        if os.path.abspath(file.name).endswith('.csv'):
            files_in_dir[(os.path.abspath(file.name))] = 0
    
        else:
            print("The file %s is not a .csv file and it will be ignored" % (file.name))
    
print(files_in_dir)

,这是通过终端执行时的错误。

 python3 sourcecheck.py -s /home/thepredator/Desktop/VOIS/csv_files/*
usage: sourcecheck.py [-h] [-s SOURCE_DIR]
sourcecheck.py: error: unrecognized arguments: /home/thepredator/Desktop/VOIS/csv_files/Book1.csv /home/thepredator/Desktop/VOIS/csv_files/Build.py /home/thepredator/Desktop/VOIS/csv_files/car_data.csv /home/thepredator/Desktop/VOIS/csv_files/db.py /home/thepredator/Desktop/VOIS/csv_files/Edited.csv /home/thepredator/Desktop/VOIS/csv_files/Iris.csv /home/thepredator/Desktop/VOIS/csv_files/Lab_Python_for_Data_Analytics.ipynb /home/thepredator/Desktop/VOIS/csv_files/startupNaN.csv /home/thepredator/Desktop/VOIS/csv_files/startupog.csv /home/thepredator/Desktop/VOIS/csv_files/text.csv

我通过观看不同的视频来编写上述代码。请帮助我解决错误。另外,我不是Python专家。

I need to achieve the following in my python module.

  1. Get a directory as an argument through the argparse module
  2. Use that directory to iterate through all the files present in that directory.
  3. Later, append only the CSV files to a dictionary and store them there.

I have tried os.walk, but it does not return anything.

I have written code to get a directory as input and then iterate through it for files. But it throws errors.

Below is the code

 import argparse
 import os
 import re
    
 # Accepting arguments for source_directory, my_sql connection details and table name to 
   update the MySQL entries to
    
parser = argparse.ArgumentParser(
        description='Utility to update MySQL tables on remote host')
parser.add_argument('-s', '--source_dir', help='Enter Path For CSV File')
    
args = parser.parse_args()
    
# Empty dictionary to store list CSV files in directory
files_in_dir = {}
    
# Function to iterate through the source_directory and read-only CSV files and append them 
   to files_in_dir dictionary
    
if args.source_dir:
    for file in args.source_dir:
    
        if os.path.abspath(file.name).endswith('.csv'):
            files_in_dir[(os.path.abspath(file.name))] = 0
    
        else:
            print("The file %s is not a .csv file and it will be ignored" % (file.name))
    
print(files_in_dir)

And this is the error when executed through the terminal.

 python3 sourcecheck.py -s /home/thepredator/Desktop/VOIS/csv_files/*
usage: sourcecheck.py [-h] [-s SOURCE_DIR]
sourcecheck.py: error: unrecognized arguments: /home/thepredator/Desktop/VOIS/csv_files/Book1.csv /home/thepredator/Desktop/VOIS/csv_files/Build.py /home/thepredator/Desktop/VOIS/csv_files/car_data.csv /home/thepredator/Desktop/VOIS/csv_files/db.py /home/thepredator/Desktop/VOIS/csv_files/Edited.csv /home/thepredator/Desktop/VOIS/csv_files/Iris.csv /home/thepredator/Desktop/VOIS/csv_files/Lab_Python_for_Data_Analytics.ipynb /home/thepredator/Desktop/VOIS/csv_files/startupNaN.csv /home/thepredator/Desktop/VOIS/csv_files/startupog.csv /home/thepredator/Desktop/VOIS/csv_files/text.csv

I have written the above code by watching different videos. Please help me fix the errors. Also, I'm not a python expert.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2025-02-01 08:50:15

我更改了files_dir = {} line

files_in_dir = { key:0 for key in os.listdir(args.source_dir) if os.path.abspath(key).endswith(".csv") }

and it seems work.

for语句版本以下请求:

for key in os.listdir(args.source_dir):
    if os.path.abspath(key).endswith(".csv"):
        files_in_dir[key] = 0
    else:
        print(f"The file {key} is not a .csv file and it will be ignored")

I changed files_dir = {} line to

files_in_dir = { key:0 for key in os.listdir(args.source_dir) if os.path.abspath(key).endswith(".csv") }

and it seems work.

if-else in for statement version following requests:

for key in os.listdir(args.source_dir):
    if os.path.abspath(key).endswith(".csv"):
        files_in_dir[key] = 0
    else:
        print(f"The file {key} is not a .csv file and it will be ignored")
守不住的情 2025-02-01 08:50:15

您可以使用此代码`

import os

files = os.listdir("/path_to_directory")    

csv = list(filter(lambda f: f.endswith('.csv'), files))

csv具有.csv扩展名的所有文件

You can use this code`

import os

files = os.listdir("/path_to_directory")    

csv = list(filter(lambda f: f.endswith('.csv'), files))

csv has all the files with .csv extension

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文