FileNotFoundError: [Errno 2] No such file or directory: When trying to move or copy file to another directory
FileNotFoundError: [Errno 2] No such file or directory: When trying to move or copy file to another directory
我正在从 excel 电子表格(文本文件列表)中读取数据,并使用每一行从使用 Pycharm Community Edition 2020 v1.3 的目录中检索实际文本文件。我设置了一个小循环来测试算法是否有效。当我尝试打印出结果时,它工作得很好,但是当我尝试将相应的文件移动或复制到另一个目录时,我得到以下输出。
Traceback (most recent call last):
File "C:/Users/dalea/PycharmProjects/untitled/d1.py", line 46, in <module>
shutil.copy(file, MDA_For_Parsing) # Move it to MDA parsing directory
File "C:\Users\dalea\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 248, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\dalea\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '19940804_10-K_edgar_data_354952_0000891618-94-000152_1.txt'
Process finished with exit code 1
搜索似乎按要求工作,问题出在复制或移动语句上。谁能解释为什么?我的代码如下。
import re
import glob
import os,sys
import csv
import shutil
import pandas as pd
import fnmatch
import string
import xlrd
from os import listdir
from os.path import isfile
MDA_Path = 'D:/1994_QTR3' # contains Loughram and MacDonald 10-K files
MDA_Target_List = r'D:/PhD_Data/Wenrui_Filing_list' # stores wenruis data
# MDA_For_Parsing = 'D:/Required_MDA_1994_QTR3' # will hold all 10-Ks from wenrui's spreadsheet once detected
MDA_For_Parsing = 'D:/Test'
# open the CSV file and extract the column containing the location of the text file(s)
datas = pd.read_excel(r'D:/PhD_Data/Wenrui_Filing_list/1994-2017filingslist_Wenrui_13Jul2020.xlsx')
df = pd.DataFrame(datas, columns = ['FILE_NAME']) # extract the data contained in FILE_NAME column
df['FILE_NAME'] = df['FILE_NAME'].str[26:] # remove the first 26 characters which contain the edgar drive info
df['FILE_NAME'] = df['FILE_NAME'].str.strip() # remove all leading and trailing
file_length = len(df) # count number of files in Wenrui's list (will need this later to loop through all occurrences)
dirs = os.listdir(MDA_Path)
for x in range(6): # if the L&M file exists in Wenrui's data set
for file in dirs:
# if file == df['FILE_NAME'][x]:
if df['FILE_NAME'][x] in file:
print(file)
shutil.copy(file, MDA_For_Parsing) # Move it to MDA parsing directory
如有任何帮助,我们将不胜感激。提前致谢。
os.listdir(MDA_Path) 仅 returns 基本文件名。名称不包括文件夹。
Ti 解决了您的问题,复制时包括路径:
shutil.copy(MDA_Path + '/' + file, MDA_For_Parsing)
我正在从 excel 电子表格(文本文件列表)中读取数据,并使用每一行从使用 Pycharm Community Edition 2020 v1.3 的目录中检索实际文本文件。我设置了一个小循环来测试算法是否有效。当我尝试打印出结果时,它工作得很好,但是当我尝试将相应的文件移动或复制到另一个目录时,我得到以下输出。
Traceback (most recent call last):
File "C:/Users/dalea/PycharmProjects/untitled/d1.py", line 46, in <module>
shutil.copy(file, MDA_For_Parsing) # Move it to MDA parsing directory
File "C:\Users\dalea\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 248, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\dalea\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '19940804_10-K_edgar_data_354952_0000891618-94-000152_1.txt'
Process finished with exit code 1
搜索似乎按要求工作,问题出在复制或移动语句上。谁能解释为什么?我的代码如下。
import re
import glob
import os,sys
import csv
import shutil
import pandas as pd
import fnmatch
import string
import xlrd
from os import listdir
from os.path import isfile
MDA_Path = 'D:/1994_QTR3' # contains Loughram and MacDonald 10-K files
MDA_Target_List = r'D:/PhD_Data/Wenrui_Filing_list' # stores wenruis data
# MDA_For_Parsing = 'D:/Required_MDA_1994_QTR3' # will hold all 10-Ks from wenrui's spreadsheet once detected
MDA_For_Parsing = 'D:/Test'
# open the CSV file and extract the column containing the location of the text file(s)
datas = pd.read_excel(r'D:/PhD_Data/Wenrui_Filing_list/1994-2017filingslist_Wenrui_13Jul2020.xlsx')
df = pd.DataFrame(datas, columns = ['FILE_NAME']) # extract the data contained in FILE_NAME column
df['FILE_NAME'] = df['FILE_NAME'].str[26:] # remove the first 26 characters which contain the edgar drive info
df['FILE_NAME'] = df['FILE_NAME'].str.strip() # remove all leading and trailing
file_length = len(df) # count number of files in Wenrui's list (will need this later to loop through all occurrences)
dirs = os.listdir(MDA_Path)
for x in range(6): # if the L&M file exists in Wenrui's data set
for file in dirs:
# if file == df['FILE_NAME'][x]:
if df['FILE_NAME'][x] in file:
print(file)
shutil.copy(file, MDA_For_Parsing) # Move it to MDA parsing directory
如有任何帮助,我们将不胜感激。提前致谢。
os.listdir(MDA_Path) 仅 returns 基本文件名。名称不包括文件夹。
Ti 解决了您的问题,复制时包括路径:
shutil.copy(MDA_Path + '/' + file, MDA_For_Parsing)