FileNotFoundError: [Errno 2] Stuck - python

Can anyone tell me why I get this error? The file is in the folder.
import os
with open("C:\\Users\\42077\\Desktop\\test\\vystup\\!output.txt", "a")as f:
for root, dirs, files in os.walk("C:\\Users\\42077\\Desktop\\test\\"):
for path in files:
if path.endswith(".txt"):
with open(path, 'r') as file:
data = file.readlines()
f.write("{0} {1}\n".format(data[2], path))

the files os.walk() returns is not a list of paths to files it's a list of names (as strings) of files in the directory where os.walk() is currently looking (root).
for path in files:
if path.endswith(".txt"):
with open(path, 'r') as file:
so at the end here open() is given a file name like example.txt.
And when open() is not given an absolute path it looks from the current working directory. Meaning it tries to find this file wherever this python file is located, and promptly gives an error.

Related

reading all the files in a directory with specific extension using glob python

I have a directory with further sub directories each having files with a specific extension. I am able to get the names of all the files using glob function:
for name in glob.glob('*/*[a-b]*'):
print(os.path.basename(name))
that prints the name of files I have in all the sub directories:
PF44_aa
PF52_aa
PF95_aa
PF38_aa
PF45_aa
PF63_aa
PF68_aa
PF39_aa
However, if I pass these file names as arguments to open the files and read the contents:
for name in glob.glob('*/*[a-b]*'):
filename=os.path.basename(name)
with open('%s' %filename) as fn:
content = fn.readlines()
I get the following error:
File "<ipython-input-194-147f38fc2684>", line 1, in <module>
with open('%s' %filename) as fn:
FileNotFoundError: [Errno 2] No such file or directory: 'PF44_aa'
I also tried giving the filename directly as input instead of %s:
for name in glob.glob('*/*[a-b]*'):
filename=os.path.basename(name)
with open(filename) as fn:
content = fn.readlines()
But still got the same error:
File "<ipython-input-193-fb125b5aa813>", line 1, in <module>
with open(filename) as fn:
FileNotFoundError: [Errno 2] No such file or directory: 'PF44_aa'
What am I doing wrong and how can I fix this?
You have to use complete path of the file to open it, you can't use just filename unless if its on the same directory as your python file. So you have to do little change in your script to make it work.
for name in glob.glob('*/*[a-b]*'):
with open(name) as fn:
content = fn.readlines()
filename is replaced by name.
here, "name" is complete path to your file.
Alternative method...
Start by first importing:
import shutil
import os
Then assign the directory to a list:
file_list = []
file_list = os.listdir('C:/filepath')
Now distinguish between files:
files = []
files = [x for x in file_list if "_aa" in x]
Now you can open and read the files in the files list using your method.
however do:
filepath + filename
with open(filepath + filename) as fn:
content = fn.readlines()
Currently you're just trying to open the file with its name, you need to include the full file path...
outcome:
"C:/filepath/PF44_aa"

How to get complete path of files that are being searched within a folder in python?

My folder structure is as follows:
I want to get the paths of all files that contain the string xyz. The result must be as:
folder/folderA/fileA2
folder/folderB/fileB1
folder/file1
I tried this:
for path, subdirs, files in os.walk(folderTestPath):
for file in files:
if "xyz" in open(folderTestPath+file,'r'):
print (os.path.abspath(file))
folderTestPath contains the path of the folder. This code only gives me the file names followed by a file not found error. I know this is a simple thing, but for some reason am unable to get it. Please help.
You can use the os.path.join method:
for path, subdirs, files in os.walk(folderTestPath):
for file in files:
filePath = os.path.join(path, file)
if "xyz" in open(filePath ,'r').read():
print("xyz")
print(filePath)
As Eric mentioned to close the file after reading it use the below snippet:
import os
for path, subdirs, files in os.walk(folderTestPath):
for file in files:
filePath = os.path.join(path, file)
with open(filePath ,'r') as data:
if "xyz" in data.read():
print("xyz")
print(filePath)
data.close()

searching and moving files using python

I have been trying to write some python code in order to get each line from a .txt file and search for a file with that name in a folder and its subfolders. After this I want to move that file in a preset destination folder.
I have tried the following code which was posted on stack overflow only but it doesn't seem to work and I am unable to figure out the problem.Any help would be highly appreciated:
import os
import shutil
def main():
destination = '/Users/jorjis/Desktop/new'
with open('/Users/jorjis/Desktop/articles.txt', 'r') as lines:
filenames_to_copy = set(line.rstrip() for line in lines)
for root, _, filenames in os.walk('/Users/jorjis/Desktop/folder/'):
for filename in filenames:
if filename in filenames_to_copy:
shutil.copy(os.path.join(root, filename), destination)
Without any debugging output (which you have now obtained) I can only guess a common pitfall of os.walk: the filenames returned in filenames are just that, filenames without any path. If your file contains filenames with paths they will never match. Use this instead:
if os.path.join(root, filename) in filenames_to_copy:
shutil.copy(os.path.join(root, filename), destination)

Python - File does not exist error

I'm trying to do a couple things here with the script below (it is incomplete). The first thing is to loop through some subdirectories. I was able to do that successfully. The second thing was to open a specific file (it is the same name in each subdirectory) and find the minimum and maximum value in each column EXCEPT the first.
Right now I'm stuck on finding the max value in a single column because the files I'm reading have two rows which I want to ignore. Unfortunately, I'm getting the following error when attempting to run the code:
Traceback (most recent call last):
File "test_script.py", line 22, in <module>
with open(file) as f:
IOError: [Errno 2] No such file or directory: 'tc.out'
Here is the current state of my code:
import scipy as sp
import os
rootdir = 'mydir'; #mydir has been changed from the actual directory path
data = []
for root, dirs, files in os.walk(rootdir):
for file in files:
if file == "tc.out":
with open(file) as f:
for line in itertools.islice(f,3,None):
for line in file:
fields = line.split()
rowdata = map(float, fields)
data.extend(rowdata)
print 'Maximum: ', max(data)
To open a file you need to specify full path. You need to change the line
with open(file) as f:
to
with open(os.path.join(root, file)) as f:
When you write open(file), Python is trying to find the the file tc.out in the directory where you started the interpreter from. You should use the full path to that file in open:
with open(os.path.join(root, file)) as f:
Let me illustrate with an example:
I have a file named 'somefile.txt' in the directory /tmp/sto/deep/ (this is a Unix system, so I use forward slashes). And then I have this simple script which resides in the directory /tmp:
oliver#armstrong:/tmp$ cat myscript.py
import os
rootdir = '/tmp'
for root, dirs, files in os.walk(rootdir):
for fname in files:
if fname == 'somefile.txt':
with open(os.path.join(root, fname)) as f:
print('Filename: %s' % fname)
print('directory: %s' % root)
print(f.read())
When I execute this script from the /tmp directory, you'll see that fname is just the filename, the path leading to it is ommitted. That's why you need to join it with the first returned argument from os.walk.
oliver#armstrong:/tmp$ python myscript.py
Filename: somefile.txt
directory: /tmp/sto/deep
contents

Python - Need to loop through directories looking for TXT files

I am a total Python Newb
I need to loop through a directory looking for .txt files, and then read and process them individually. I would like to set this up so that whatever directory the script is in is treated as the root of this action. For example if the script is in /bsepath/workDir, then it would loop over all of the files in workDir and its children.
What I have so far is:
#!/usr/bin/env python
import os
scrptPth = os.path.realpath(__file__)
for file in os.listdir(scrptPth)
with open(file) as f:
head,sub,auth = [f.readline().strip() for i in range(3)]
data=f.read()
#data.encode('utf-8')
pth = os.getcwd()
print head,sub,auth,data,pth
This code is giving me an invalid syntax error and I suspect that is because os.listdir does not like file paths in standard string format. Also I dont think that I am doing the looped action right. How do I reference a specific file in the looped action? Is it packaged as a variable?
Any help is appriciated
import os, fnmatch
def findFiles (path, filter):
for root, dirs, files in os.walk(path):
for file in fnmatch.filter(files, filter):
yield os.path.join(root, file)
Use it like this, and it will find all text files somewhere within the given path (recursively):
for textFile in findFiles(r'C:\Users\poke\Documents', '*.txt'):
print(textFile)
os.listdir expects a directory as input. So, to get the directory in which the script resides use:
scrptPth = os.path.dirname(os.path.realpath(__file__))
Also, os.listdir returns just the filenames, not the full path.
So open(file) will not work unless the current working directory happens to be the directory where the script resides. To fix this, use os.path.join:
import os
scrptPth = os.path.dirname(os.path.realpath(__file__))
for file in os.listdir(scrptPth):
with open(os.path.join(scrptPth, file)) as f:
Finally, if you want to recurse through subdirectories, use os.walk:
import os
scrptPth = os.path.dirname(os.path.realpath(__file__))
for root, dirs, files in os.walk(scrptPth):
for filename in files:
filename = os.path.join(root, filename)
with open(filename, 'r') as f:
head,sub,auth = [f.readline().strip() for i in range(3)]
data=f.read()
#data.encode('utf-8')

Categories

Resources