It is because os.listdir
does not return the full path to the file, only the filename part; that is 'foo.txt'
, when open would want 'E:/somedir/foo.txt'
because the file does not exist in the current directory.
Use os.path.join
to prepend the directory to your filename:
path = r'E:/somedir' for filename in os.listdir(path): with open(os.path.join(path, filename)) as f: ... # process the file
(Also, you are not closing the file; the with
block will take care of it automatically).
Related Posts:
- IndexError: too many indices for array
- IndexError: too many indices for array
- How to print an exception in Python?
- Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- IndexError: too many indices for array
- Numpy std calculation: TypeError: cannot perform reduce with flexible type
- Failed to load the native TensorFlow runtime – TensorFlow 2.1
- Error handling in Python-MySQL
- PHP Notice: Undefined offset: 1 with array when reading data
- What does ‘index 0 is out of bounds for axis 0 with size 0’ mean?
- What does ‘index 0 is out of bounds for axis 0 with size 0’ mean?
- How to make a histogram from a list of data
- How to throw error and exit with a custom message in python
- Handling a timeout error in python sockets
- What does “while True” mean in Python?
- TypeError: ‘str’ object is not callable (Python)
- Converting string into datetime
- ValueError: invalid literal for int() with base 10: ”
- How do I list all files of a directory?
- Print string to text file
- How can I implement a tree in Python?
- How can I parse a YAML file in Python
- Cannot find module cv2 when using OpenCV
- Maximum and Minimum values for ints
- How is Anaconda related to Python?
- What is the python keyword “with” used for? [duplicate]
- What is the purpose of meshgrid in Python / NumPy?
- PermissionError: [Errno 13] in Python
- Why is it string.join(list) instead of list.join(string)?
- Importing class from another file [duplicate]
- Pandas group-by and sum
- Is it possible to compile a program written in Python?
- Python List vs. Array – when to use?
- How to terminate a script?
- numpy array concatenate: “ValueError: all the input arrays must have same number of dimensions”
- csv.Error: iterator should return strings, not bytes
- How to print to stderr in Python?
- SyntaxError: non-default argument follows default argument
- socket.error: [Errno 48] Address already in use
- Python Key Error=0 – Can’t find Dict error in code
- How can I flush the output of the print function?
- Python calling method in class
- No module named pkg_resources
- Opening a .ipynb.txt File
- How do I remove all packages installed by PIP?
- SyntaxError: cannot assign to operator
- Using a comparator function to sort
- How to increase plt.title font size?
- ‘list’ object has no attribute ‘shape’
- How do I find the duplicates in a list and create another list with them?
- What is the meaning of single and double underscore before an object name?
- What is the difference between np.mean and tf.reduce_mean?
- How do I combine two dataframes?
- How can I get list of values from dict?
- How do I append one string to another in Python?
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to sort a list of objects based on an attribute of the objects?
- Loading a file with more than one line of JSON into Pandas
- SystemError: new style getargs format but argument is not a tuple?
- How to convert bytearray to string in python
- Subtracting two lists in Python
- Is there any way to kill a Thread?
- BeautifulSoup getting href
- Equivalent of shell ‘cd’ command to change the working directory?
- What is PEP8’s E128: continuation line under-indented for visual indent?
- PyQt is thread-safe to the same extent that Qt is thread-safe. The Qt docs will tell you which parts of their API are guaranteed to be so, and under what circumstances.
- How to downgrade the installed version of ‘pip’ on windows?
- Where do I get a SECRET_KEY for Flask?
- What is the preferred way to include error messages in C++?
- Python NameError, variable ‘not defined’
- Cannot import cv2 in python in OSX
- numpy-equivalent of list.pop?
- How to get row number in dataframe in Pandas?
- Difference between Python’s Generators and Iterators
- “Unorderable types: int() < str()"
- python : an integer is required (got type str)
- Django Rest Framework — no module named rest_framework
- Unsupported operation :not writeable python
- RuntimeError: Invalid DISPLAY variable
- bash: mkvirtualenv: command not found
- Convert a Unicode string to a string in Python (containing extra symbols)python string unicode type-conversion
- Python Django: You’re using the staticfiles app without having set the STATIC_ROOT setting
- sqlite3.OperationalError: unable to open database file
- Object of type ‘map’ has no len() in Python 3
- Disable / Enable Button in TKinter
- Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support
- How do I find the time difference between two datetime objects in python?
- How to strip a specific word from a string?
- Printing subscript in python
- Get difference between two lists
- how to build .exe for python 3.5+, 3.6 if possible?
- How to query as GROUP BY in django?
- Python regex match space only
- OpenCV houghLinesP parameters
- Generate a random letter in Python
- NameError from Python input() function
- How do I tell Matplotlib to create a second (new) plot, then later plot on the old one?
- Python Pandas : pivot table with aggfunc = count unique distinct
- Flask Value error view function did not return a response [duplicate]
- AttributeError: ‘Series’ object has no attribute ‘split’ error in sending emails