我有一个要读取的csv文件目录,提取所需的信息,然后将其另存为另一个目录中的csv文件。我已经定义了代码块来完成所需的过程,但是在尝试测试一个文件时遇到了“无文件或目录”错误。据我所知,我正在代码中创建所需的文件,因此我不理解为什么文件不存在。
def writeFormPage(file, path):
'''
Input:
Index CSV
Output:
Page CSV
'''
with open(file, 'r') as rf:
reader = csv.reader(rf)
base_name = os.path.basename(file)
file_path = os.path.join(path, base_name)
with open(file_path, 'w') as wf:
writer = csv.writer(wf, delimiter = ',')
for line in reader:
url = line[-1]
page_data = (parseFormPage(url))
writer.writerow(page_data)
time.sleep(3 + random.random() * 3)
os.chdir(PAGE_DIR)
demo_index_csv = '/Users/alexajones/index/2018Q4.csv'
# testing!!!
writeFormPage(demo_index_csv, PAGE_DIR)
FileNotFoundError Traceback (most recent call last)
<ipython-input-14-81e495544fd6> in <module>
4
5 # testing!!!
----> 6 writeFormPage(demo_index_csv, PAGE_DIR)
<ipython-input-12-ff5447b72e24> in writeFormPage(file, path)
13 file_path = os.path.join(path, base_name)
14
---> 15 with open(file_path, 'w') as wf:
16 writer = csv.writer(wf, delimiter = ',')
17
FileNotFoundError: [Errno 2] No such file or directory: './page/2018Q4.csv'
我绝对可以确定我犯了一些愚蠢的错误,因为我是一个绝对的初学者,但是到目前为止我还没有找到它。我想确保代码能正常工作,因为最终我将编写一个循环以完成目录中所有csv文件的处理。任何帮助将不胜感激。
回应Barmar和Martineau的回答;我已经在程序中进一步创建了该子目录,一次创建了3个目录,尽管这些子目录存在很多“不存在”的问题。根据您的建议,我决定按需创建每个目录,如下所示,尽管我现在在一段可以正常工作的代码中出现“不存在”错误。这些目录在做什么错?附带说明,我需要这些在其他人的PC上工作,所以我无法指定整个路径。
# Create a new directory to hold CSV files
indx_dir = './index'
if not os.path.isdir(indx_dir):
os.makedirs(indx_dir)
os.chdir(indx_dir)
#Create new list to store URLs from csv files
sec_urls = []
# For each csv file, open and locate URL in line 4 and add to newly created list
for filename in os.listdir(indx_dir):
if filename.endswith('.csv'):
with open(os.path.join(indx_dir, filename), newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for line in reader:
url = line[4].strip()
sec_urls.append(url)
print(url, 'downloaded and added to list')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-24-281ad84b64c7> in <module>
3
4 # For each csv file, open and locate URL in line 4 and add to newly created list
----> 5 for filename in os.listdir(indx_dir):
6 if filename.endswith('.csv'):
7 with open(os.path.join(indx_dir, filename), newline='') as csvfile:
FileNotFoundError: [Errno 2] No such file or directory: './index'
谢谢巴尔马尔,这个问题现在已经解决。我最初询问的问题的代码已更改为:
# Define a function to collect form page data and save as a new csv file
def writeFormPage(file, path):
'''
Input:
Index CSV
Output:
Page CSV
'''
with open(file, 'r') as rf:
reader = csv.reader(rf)
base_name = os.path.basename(file)
file_path = os.path.join(path, base_name)
with open(file_path, 'w') as wf:
writer = csv.writer(wf, delimiter = ',')
for line in reader:
url = line[-1]
page_data = (parseFormPage(url))
writer.writerow(page_data)
time.sleep(3 + random.random() * 3)
# Create a new directory to save new CSV files
page_dir = './page'
if not os.path.isdir(page_dir):
os.makedirs(page_dir)
os.path.isdir(page_dir)
demo_index_csv = './index/2018Q4.csv'
# testing!!!
writeFormPage(demo_index_csv, page_dir)