Open files within same directory - python

I tried opening a file and it says, FileNotFoundError: [Errno 2] No such file or directory: 'words.txt' although it is in the same directory, here is the code I am using:
words = []
with open("words.txt", "r") as file:
words_string = file.read()
words = words_string.split()

Use os.listdir() to list the files in the current working directory and check if the desired file is present.
To print the path of current working directory use os.getcwd().
I would recommend to use absolute paths instead of relative path to make the code clean and less prone to errors. If you don't want to use the absolute paths then use os.chdir(directory_name) to change the current working directory during runtime.

Related

Python cannot find exiting path

While preparing my dataset for a machine learning model I stumbled upon a bug (at least from my perspective). I was trying to move files around in Windows 10 but my system always told me that my file does not exist (FileNotFoundError: [Errno 2] No such file or directory: '../path/to/file'), but I specifically probed for the file before which returned True.
What I was trying in my IPython console was the following (which can reproduce the bug):
import os
import shutil
path = '../path/to/file' # You can see I use relative paths
out_path = '../path/to/out/'
os.makedirs(out_path) # Make sure paths do exist
_, name = os.path.split(path)
out_path += name
# Produces almost the same error FileNotFoundError: [WinError 3] The system cannot find the given directory: path
# os.rename(path, out_path)
shutil.move(path, out_path)
You obviously need a file to move at '../path/to/file'. I tested whether I use the correct path with os.path.exist(path) which returned True.
Do I simply misread the documentation and the usage is different to what I am thinking? I understood it like the first argument is the path to the file to move and the second one is the path for the file after moving it.
Or is there really a bug in my Python environment?
I also tried moving the file with absolute paths returned by os.path.abspath(), but this did not work either.
FYI: I am using Windows 10 and Python 3.8.12.
Edit: I found the mistake I did... After 2.5 days of labeling image data it appears like I had an underscore mistaken for a hyphen which is why my output directory seems to be malformed... :/
You are not doing a relative path to the current directory, but to the parent directory of the current directory with a double dot '../path' and not single dot './path'. So if your files aren't in the parent directory of the current directory, they won't be found.
When you try to append the file name to the directory path "out_path" you do a string append. which just makes a new file with the combined names rather than move the file into the directory. Use the os.path.join() function for this.
Here is code I made that corrects these two points and works on my end.
import os
import shutil
path = './somefile.txt' # CORRECTED: One dot for current directory
out_path = './somedir'
try:
os.makedirs(out_path) # Make sure paths do exist
except Exception as e:
print(e)
_, name = os.path.split(path)
out_path = os.path.join(out_path, name) # CORRECTED: Use appropriate function
# Produces almost the same error FileNotFoundError: [WinError 3] The system cannot find the given directory: path
# os.rename(path, out_path)
shutil.move(path, out_path)

FileNotFoundError: [Errno 2] No such file or directory: 'demofile.txt'

can someone tell me why when I want to open a text file with python
my output is FileNotFoundError: [Errno 2] No such file or directory: 'demofile.txt'
even though the file is already in the same folder and the writing is correct.
and this is my code
f = open("demofile.txt", "r")
print(f.read())
thank you before
The path of the Python file and the current working directory can differ. open uses the current working directory if you use a relative path. The obvious fix is to use an absolute path. But then you will have to edit the code each time you copy the script to a different folder.
You can use pathlib to create an absolute path based on the current running scripts location. Put this in a Python script, run it and look at the result.
import pathlib
print(pathlib.Path(__file__).parent)
print(pathlib.Path(__file__).parent / 'demofile.txt')
print(pathlib.Path(__file__).parent / 'data' / 'demofile.txt')
So your code can be changed to
filepath = pathlib.Path(__file__).parent / 'demofile.txt'
with open(filepath, 'r') as f:
print(f.read())
Try using with
with open("demofile.txt","r") as f:
f.read()
Maybe, You are executing python file with absolute path that's why FileNotFound.
Try with absolute path. Like, c:\files\demofile.txt
I solved the problem with the answer of Mathias, I had the same problem but with an image, then you need to write
import pathlib
print(pathlib.Path(__file__).parent)
print(pathlib.Path(__file__).parent / 'name_of_your_file.extension')
print(pathlib.Path(__file__).parent / 'data' / 'name_of_your_file.extension')
already done the correct answer is to use the name of the specific folder where the text files are located
thank you very much, everyone

FileNotFoundError Using listdir in python

so I have a directory with a few subdirectories in it and I am trying to iterate through all the subdirectories I have (each subdirectory has a bunch of files in it that I'm splitting up into smaller files). i've been trying to use os.listdir but I keep getting this error FileNotFoundError: [Errno 2] No such file or directory: 'mFAPA'
This subdirectory definitely exists, so I'm not sure why this keeps happening
for dir in os.listdir('../conv_files'):
for filename in os.listdir(dir):
I was trying to use the for loops to go through each directory and then in each directory go through each file. The error is on the second line of code, once it's in the parent directory it for some reason can't do the for filename in os.listdir(dir) part. Any suggestions?
You can use os.walk() which traverses into each subdirectory and files inside a given directory. Refer https://www.geeksforgeeks.org/os-walk-python/ for more details
for (root,dirs,files) in os.walk('../conv_files'):
#add your code here
syntax: os.listdir(path)
Parameters:
path (optional) : path of the directory
Return Type: This method returns the list of all files and directories in the specified path. The return type of this method is list.
in your first nested loop, it consists of the file names but os.listdir(path) requires path in it.

Working with file directories and variables

I have a program that I want to make more dynamic. The current setup is literally typing everything out.
I would like the program to work with a for loop (any other suggestions would be great). My goal is to loop through a specific file that has sub-directories and get the name of each folder (sub-directory), then get the name of the files within the sub-directory.
To put this in a file string:
C:\Folder 1\Folder 2\File name
From the above, I would like to get the value of Folder 2 and File name.
My code so far:
for sub_dir in os.listdir(r"C:\Folder 1\"):
DIR = r'' + sub_dir
files_found = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
if(files_found > 0):
for files in os.listdir(sub_dir):
file_name = os.path.splitext(files)[0]
I get the error --> FileNotFoundError: [WinError 3] The system cannot find the path specified: Folder 2
I appreciate your efforts to help.
You should take a look at os.walk
Have a look at os.walk()
It recursively traverses a file tree, returning a list of all directories and files within a directory at each step.

Adding actual files to a list, instead of just the file's string name

I am having issues reading the contents of the files I am trying to open due to the fact that python believes there is:
"No such file or directory: 'Filename.xrf'"
Here is an outline of my code and what I think the problem may be:
The user's input defines the path to where the files are.
direct = str(raw_input("Enter directory name where your data is: ))
path = "/Users/myname/Desktop/heasoft/XRF_data/%s/40_keV" \
%(direct)
print os.listdir(path)
# This lists the correct contents of the directory that I wanted it to.
So here I essentially let the user decide which directory they want to manipulate and then I choose one more directory path named "40_keV".
Within a defined function I use the OS module to navigate to the corresponding directory and then append every file within the 40_keV directory to a list, named dataFiles.
def Spectrumdivide():
dataFiles = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.xrf'):
dataFiles.append(file)
Here, the correct files were appended to the list 'dataFiles', but I think this may be where the problem is occurring. I'm not sure whether or not Python is adding the NAME of the file to my list instead of the actual file object.
The code breaks because python believes there is no such file or directory.
for filename in dataFiles:
print filename
f = open(filename,'r') # <- THE CODE BREAKS HERE
print "Opening file: " + filename
line_num = f.readlines()
Again, the correct file is printed from dataFiles[0] in the first iteration of the loop but then this common error is produced:
IOError: [Errno 2] No such file or directory: '40keV_1.xrf'
I'm using an Anaconda launcher to run Spyder (Python 2.7) and the files are text files containing two columns of equal length. The goal is to assign each column to a list and the manipulate them accordingly.
You need to append the path name not just the file's name using the os.path.join function.
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.xrf'):
dataFiles.append(os.path.join(root, file))

Categories

Resources