# -*- coding: utf-8 -*-

"""

Created on Mon Aug 19 22:06:43 2019

@author: Om

"""

import os

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import datetime

from datetime import timedelta

# Define the symbol list

symbol = []

# Select file to access

filename=input('Enter the name of file to access')

filename=filename+'.txt'

print(filename)

with open('D:\\om\\python_stock_script\\filename')as f:

for line in f:

symbol.append(line.strip())

f.close

我得到以下错误:

Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]

Type "copyright", "credits" or "license" for more information.

IPython 7.6.1 -- An enhanced Interactive Python.

runfile('D:/Om/python_stock_script/StockEval.py', wdir='D:/Om/python_stock_script')

Enter the name of file to accessDivStock

DivStock.txt

Traceback (most recent call last):

File "", line 1, in

runfile('D:/Om/python_stock_script/StockEval.py', wdir='D:/Om/python_stock_script')

File "C:\Users\Om\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile

execfile(filename, namespace)

File "C:\Users\Om\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile

exec(compile(f.read(), filename, 'exec'), namespace)

File "D:/Om/python_stock_script/StockEval.py", line 22, in

with open('D:\\om\\python_stock_script\\filename')as f:

FileNotFoundError: [Errno 2] No such file or directory: 'D:\\om\\python_stock_script\\filename'

如果我替换文件名DivStock.txt文件“代码起作用。文件存在于指定的目录中,因为我将它放在那里。我尝试了几种方法,包括“pathlib”。显然我做错了什么。我想更能选择要选择的文件。请帮忙