Opening all files in a directory - Python [duplicate] - python

This question already has answers here:
How to identify whether a file is normal file or directory
(7 answers)
Closed 5 years ago.
I found some example code for ClamAV. And it works fine, but it only scans a single file. Here's the code:
import pyclamav
import os
tmpfile = '/home/user/test.txt'
f = open(tmpfile, 'rb')
infected, name = pyclamav.scanfile(tmpfile)
if infected:
print "File infected with %s Deleting file." %name
os.unlink(file)
else:
print "File is clean!"
I'm trying to scan an entire directory, here's my attempt:
import pyclamav
import os
directory = '/home/user/'
for filename in os.listdir(directory):
f = open(filename, 'rb')
infected, name = pyclamav.scanfile(filename)
if infected:
print "File infected with %s ... Deleting file." %name
os.unlink(filename)
else:
print " %s is clean!" %filename
However, I'm getting the following error:
Traceback (most recent call last):
File "anti.py", line 7, in <module>
f = open(filename, 'rb')
IOError: [Errno 21] Is a directory: 'Public'
I'm pretty new to Python, and I've read several similar questions and they do something like what I did, I think.

os.listdir("DIRECTORY") returns list of all files/dir in the DIRECTORY . It is just file names not absolute paths. So, if You are executing this program from a different directory it's bound to fail.
If you are sure that everything in the directory is a file, no sub directories. You can try following,
def get_abs_names(path):
for file_name in os.listdir(path):
yield os.path.join(path, file_name)
Then ,
for file_name in get_abs_names("/home/user/"):
#Your code goes here.

The following code will go over all your directory file by file. Your error happens because you try to open a directory as if it is a file instead of entering the dir and opening the files inside
for subdir, dirs, files in os.walk(path): # walks through whole directory
for file in files:
filepath = os.path.join(subdir, file) # path to the file
#your code here

Related

How to get name of random .jpg in folder? Python [duplicate]

I was trying to iterate over the files in a directory like this:
import os
path = r'E:/somedir'
for filename in os.listdir(path):
f = open(filename, 'r')
... # process the file
But Python was throwing FileNotFoundError even though the file exists:
Traceback (most recent call last):
File "E:/ADMTM/TestT.py", line 6, in <module>
f = open(filename, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'foo.txt'
So what is wrong here?
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).
os.listdir(directory) returns a list of file names in directory. So unless directory is your current working directory, you need to join those file names with the actual directory to get a proper absolute path:
for filename in os.listdir(path):
filepath = os.path.join(path, filename)
f = open(filepath,'r')
raw = f.read()
# ...
Here's an alternative solution using pathlib.Path.iterdir, which yields the full paths instead, removing the need to join paths:
from pathlib import Path
path = Path(r'E:/somedir')
for filename in path.iterdir():
with filename.open() as f:
... # process the file

I keep getting FileNotFoundError [duplicate]

This question already has an answer here:
using os.remove() in os.walk() for loop returns FileNotFoundError
(1 answer)
Closed 11 months ago.
This is the code
import os
new_file = open("C:/Users/USER/Desktop/Coding/Python/element_search.txt", "w")
path = "C:/Users/USER/Desktop/Coding"
# This is to access sub-folders
dirs = os.listdir(path)
for root, dir, files in os.walk(path):
for file in files:
f = open(file)
content = f.read()
print(file)
And this is the error
C:\Users\USER\Desktop\Coding\Python\personal_projects\venv\Scripts\python.exe C:/Users/USER/Desktop/Coding/Python/personal_projects/element_search.py
Traceback (most recent call last):
File "C:\Users\USER\Desktop\Coding\Python\personal_projects\element_search.py", line 10, in <module>
f = open(file)
FileNotFoundError: [Errno 2] No such file or directory: 'launch.json'
But I have the file launch.json present.
The os.walk returns the walking directory as root, and the list of file names. You need to construct the full path of the file for opening, else python will search the file only in working directory.
You can construct the full path with pathlib or os.join. pathlib is the recommended option. See an edited approach below.
import os
import pathlib
new_file = open("C:/Users/USER/Desktop/Coding/Python/element_search.txt", "w")
path = "C:/Users/USER/Desktop/Coding"
# This is to access sub-folders
dirs = os.listdir(path)
# root is the parent directory and files is the list of names returned.
for root, _, files in os.walk(path): # do not use the dir as its a builtin keyword
for file in files:
# join the name and dir path, to make it readable
abs_file = pathlib.Path(root) / file # Prepare the absolute path for the file
with open(abs_file) as f: # Open with a context, so it closes after use
content = f.read()
print(content)
If you have binary files in the list, then you must provide the read as binary flag in opening like with open(abs_file, "rb")

Moving text file from one folder to another folder using python in windows

I am trying to move text files from one folder to another by reading a path from a csv file. First I create the target folder in which I want to move my files from the existing folder. I read the existing folder path from csv file. I am working on a Windows platform.
This is my code :
import os
import csv
import shutil
#csv_filename = raw_input('Enter CSV filename:')
with open('insurance_sample.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter = ';')
header = next(readCSV)
count = 0
for row in readCSV:
dirname = "/".join(('Sorted_Program',row[1],row[4],row[3],row[7]))
#if not os.path.exists(dirname):
#os.makedirs(dirname)
path = row[10]
moveto = dirname
print path
print moveto
print os.path.isfile(path)
files = os.listdir(path)
print files
files.sort()
for f in files:
src = path + f
dst = moveto + f
break
I am getting this error after running the code:
C:\Users\Ashwin\Desktop\p\newDir\Archives\Beta\A380_1
Sorted_Program/A380/AFR/69/Flight_Test
False
Traceback (most recent call last):
File "C:\Users\Ashwin\Desktop\p\newDir\sar.py", line 19, in <module>
files = os.listdir(path)
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\Users\\Ashwin\\Desktop\\p\\newDir\\Archives\\Beta\\A380_1/*.*
Please let me know if the question is still confusing and I will try to explain in more detail.
So it seems there are two issues here:
You are creating a directory reference with / whereas windows directories require \
Your code does not prefix the new directory structure with \
Try the following modification:
dirname = "\\" + "\\".join(('Sorted_Program',row[1],row[4],row[3],row[7]))

python - open all plain text files in a directory with ".dta" extension and write lines to csv

I have a number of plain-text config files (.dta) that are spread through 27 sub-directories. I am trying to parse some information from all of them into a common document that is easier to work with.
Thus far I have:
import linecache
import csv
import os
csvout = csv.writer(open("dtaCompile.csv","wb"))
directory = os.path.join("c:\\","DirectKey")
for root,dirs,files in os.walk(directory):
for file in files:
if file.endswith(".DTA"):
f=open(file,'r')
lines = f.readlines()
description = lines[1]
articleCode = lines[2]
OS = lines[25]
SMBIOS = lines[32]
pnpID = lines[34]
cmdLine = lines[28]
csvout.writerow([SMBIOS, description, articleCode, pnpID, OS, cmdLine])
f.close()
I'm getting the following error:
Traceback (most recent call last):
File "test.py", line 11, in <module>
f=open(file,'r')
IOError: [Errno 2] No such file or directory: '000003APP.DTA'
Instead of
f=open(file,'r')
Your probaby need
f=open(os.path.join(directory, root, file),'r')
file is just the name of the file, and doesn't say anything about the path to it. you have to use os.path.join with the various components to create the full path
if file.endswith(".DTA"):
file = os.path.join(directory, root, file)
Instead of:
f=open(file,'r')
Try:
f = open(os.path.join(directory, file), "r")
My guess is that the directory you're program is executing in is not the same as the directory you're walking.
Try printing:
os.getcwd()
to see.

Python 2.5.2: trying to open files recursively

The script below should open all the files inside the folder 'pruebaba' recursively but I get this error:
Traceback (most recent call last):
File
"/home/tirengarfio/Desktop/prueba.py",
line 8, in
f = open(file,'r') IOError: [Errno 21] Is a directory
This is the hierarchy:
pruebaba
folder1
folder11
test1.php
folder12
test1.php
test2.php
folder2
test1.php
The script:
import re,fileinput,os
path="/home/tirengarfio/Desktop/pruebaba"
os.chdir(path)
for file in os.listdir("."):
f = open(file,'r')
data = f.read()
data = re.sub(r'(\s*function\s+.*\s*{\s*)',
r'\1echo "The function starts here."',
data)
f.close()
f = open(file, 'w')
f.write(data)
f.close()
Any idea?
Use os.walk. It recursively walks into directory and subdirectories, and already gives you separate variables for files and directories.
import re
import os
from __future__ import with_statement
PATH = "/home/tirengarfio/Desktop/pruebaba"
for path, dirs, files in os.walk(PATH):
for filename in files:
fullpath = os.path.join(path, filename)
with open(fullpath, 'r') as f:
data = re.sub(r'(\s*function\s+.*\s*{\s*)',
r'\1echo "The function starts here."',
f.read())
with open(fullpath, 'w') as f:
f.write(data)
You're trying to open everything you see. One thing you tried to open was a directory; you need to check if an entry is a file or is a directory, and make a decision from there. (Was the error IOError: [Errno 21] Is a directory not descriptive enough?)
If it is a directory, then you'll want to make a recursive call to your function to walk over the files in that directory as well.
Alternatively, you might be interested in the os.walk function to take care of the recursive-ness for you.
os.listdir lists both files and directories. You should check if what you're trying to open really is a file with os.path.isfile

Categories