写代码以迭代目录中的所有文件,并将每个文件的词典路径附加
我需要在Python模块中实现以下内容。
- 通过ARGPARSE模块使用该目录作为参数,
- 该目录在该目录中的所有文件中迭代目录。
- 后来,仅将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.
- Get a directory as an argument through the argparse module
- Use that directory to iterate through all the files present in that directory.
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我更改了
files_dir = {}
linefiles_in_dir = { key:0 for key in os.listdir(args.source_dir) if os.path.abspath(key).endswith(".csv") }
and it seems work.
for语句版本以下请求:
I changed
files_dir = {}
line tofiles_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:
您可以使用此代码`
csv具有.csv扩展名的所有文件
You can use this code`
csv has all the files with .csv extension