Looping over csv files in a Python directory - python

I'm running this script to loop over a directory of CSV files to run cross validation on.
for filename in os.listdir("/Users/name/PycharmProjects/Project/Data/Nod"):
k_fold(filename)
I get the error:
Traceback (most recent call last):
File "/path_of_cross_validation_file", line 28, in <module>
k_fold(filename)
File "/path_of_cross_validation_file", line 7, in k_fold
data = open(myfile).readlines()
IOError: [Errno 2] No such file or directory: 'file_name.csv'
How do I iterate through all these files to split the data into training and testing files?
For reference, a file in Nod could look like this:
x,y,z
-1.3518261999999999,0.19841946999999999,0.058807577999999999
-1.5427636999999998,0.54079030000000006,-0.15981296
-1.4453497,0.04129998,0.046387657999999998
-1.4743793000000001,-0.064793080000000003,0.18315643000000001

It turns out I needed to use the glob module.
Here's the solution:
for filename in glob.iglob('Path_to_directory/*.csv'):
k_fold(filename)

Related

Combining PDFs errno 2 no such file or directory even the file exists python

I'm combining numerous PDF files in Python and getting the error 'errno 2 nno such file or directory' despite the fact that the file exists.
I tried to display the PDF files just to show that the PDF file exists
import os
from PyPDF2 import PdfFileMerger
merger = PdfFileMerger()
source_dir = os.getcwd() + '/Combined PDF'
for items in os.listdir(source_dir):
if items.endswith('.pdf'):
print(items)
#merger.append(items)
#merger.write('./Combined PDF/CombinedPDF.pdf')
#merger.close()
Result
PS C:\Users\RVC\Desktop\Python> & C:/Users/RVC/AppData/Local/Microsoft/WindowsApps/python3.9.exe "c:/Users/RVC/Desktop/Python/Combined PDF/test.py"
PDF 1.pdf
PDF 2.pdf
PDF 3.pdf
PDF 4.pdf
But when I removed the comment # and execute it failed
Traceback (most recent call last):
File "c:\Users\RVC\Desktop\Python\Combined PDF\test.py", line 11, in <module>
merger.append(items)
File "C:\Users\RVC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PyPDF2\merger.py", line 203, in append
self.merge(len(self.pages), fileobj, bookmark, pages, import_bookmarks)
File "C:\Users\RVC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PyPDF2\merger.py", line 114, in merge
fileobj = file(fileobj, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'PDF 1.pdf'
PS C:\Users\RVC\Desktop\Python>
You are appending PDF 1.pdf etc. which means merger expects those files to be in cwd.

python3 try to handle multiple different zipped pcap files

I try to find a solution to work (just read) a PCAP/pcap File inside a ZIP Archive.
I won´t unzip the File to disk, because there are 1000 of zips, and each of them has one pcap inside
e.G.
file1.zip => contains file1.pcap
file2.zip => contains file2.pcap ...
I am using pyshark atm but can also use dpkt,scapy if it works with memory zips. But pyshark try to find the file.
>>> with ZipFile(pcaps_zip, "r") as z_in:
... for pcap in z_in.namelist():
... pyshark.FileCapture(pcap)
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/local/lib/python3.6/site-packages/pyshark/capture/file_capture.py", line 48, in
__init__
+ str(self.input_filename)
FileNotFoundError: [Errno 2] No such file or directory: file1.pcap

Failing to make an array of Images in Python

I am trying to create an array of .jpg files, but the compiler is not building the array
More specifically, my problem is that a public folder, whose path is defined as the object path, is not accessible by my Python compiler [Spyder]. However, the folder, and its respective files are all public and open access to everyone. What might be the reason that my computer cannot access the images?
Code 1 is an simple function to find and access the file path I want, and the Kernal results show what is failing.
Code 2 is the syntax for the isolated error in the program I am applying the open() method. Kernal results depict compiler failure.
Code 1:
import os
path = r'C:/Users/BeckerLab/Pictures/Final_Sample_Set/Right2'
try:
os.path.exists(path)
if (True):
R = open(path)
R.close()
except FileNotFoundError:
print("file does not exist")
Kernal for Code 1:
!runfile('C:/Users/BeckerLab/untitled6.py', wdir='C:/Users/BeckerLab')
Traceback (most recent call last):
File "C:\Users\BeckerLab\untitled6.py", line 8, in <module>
R = open(path)
PermissionError: [Errno 13] Permission denied: 'C:/Users/BeckerLab/Pictures/Final_Sample_Set/Right2'
Code 2:
import os
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
Kernal Results for Code 2:
!runfile('C:/Users/BeckerLab/almost.py', wdir='C:/Users/BeckerLab')
2020-04-05 12:59:28
Traceback (most recent call last):
File "C:\Users\BeckerLab\almost.py", line 46, in <module>
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
File "C:\Users\BeckerLab\almost.py", line 46, in <listcomp>
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
FileNotFoundError: [Errno 2] No such file or directory: 'R1.JPG'
Notice that your condition is:
os.path.exists(path)
if (True):
which will always be true. Maybe try:
if (os.path.exists(path)):
Try moving the files to another directory like 'D:/../BeckerLab/untitled6.py'

How do I change/choose the file path in Python?

I'm trying to access a .txt file in Python and I can't figure out how to open the file. I ended up copying the contents into a list directly but I would like to know how to open a file for the future.
If I run this nothing prints. I think it's because Python is looking in the wrong folder/directory but I don't know how to change file paths.
sourcefile = open("CompletedDirectory.txt").read()
print(sourcefile)
The file CompletedDirectory.txt is probably empty.
If Python could not find the file, you would get a FileNotFoundError exception:
>>> sourcefile = open("CompletedDirectory.txt").read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'CompletedDirectory.txt'
Note that using read() in this way is not recommended. You're not closing the file properly. Use a context manager:
with open("CompletedDirectory.txt") as infile:
sourcefile = infile.read()
This will automatically close infile on leaving the with block.
You can get the current working directory:
import os
os.getcwd()
Then just concat it with the file container directory
os.path.join("targetDir", "fileName")

IO error in savetxt while using numpy

Im trying to read a dataset and collect meta features from it.
I get the following error after executing the python file.
Traceback (most recent call last):
File "runmeta.py", line 79, in <module>
np.savetxt('datasets/'+str(i)+'/metafeatures',meta[i],delimiter=',')
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 940, in savetxt
fh = open(fname, 'w')
IOError: [Errno 2] No such file or directory: 'datasets/2/metafeatures'
the error you're getting is simply telling you it didn't find the file. i would suggest looking into absolute and relative file paths.
advice in error handling:
the error is triggered on this line
fh = open(fname, 'w')
so as you debug your program, look at the line python shows you. maybe change the variable fname. that is where i would start.
currently
fname = 'datasets/2/metafeatures'

Categories