How to Open Every File in a Folder

How to open every file in a folder

Os

You can list all files in the current directory using os.listdir:

import os
for filename in os.listdir(os.getcwd()):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff

Glob

Or you can list only some files, depending on the file pattern using the glob module:

import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff

It doesn't have to be the current directory you can list them in any path you want:

import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff

Pipe

Or you can even use the pipe as you specified using fileinput

import fileinput
for line in fileinput.input():
# do your stuff

And you can then use it with piping:

ls -1 | python parse.py

loop through folder in python and open files throws an error

os.listdir() gives you only the filename, but not the path to the file:

import os

for filename in os.listdir('path/to/dir'):
if filename.endswith('.log'):
with open(os.path.join('path/to/dir', filename)) as f:
content = f.read()

Alternatively, you could use the glob module. The glob.glob() function allows you to filter files using a pattern:

import os
import glob

for filepath in glob.glob(os.path.join('path/to/dir', '*.log')):
with open(filepath) as f:
content = f.read()

Find all files in a directory with extension .txt in Python

You can use glob:

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)

or simply os.listdir:

import os
for file in os.listdir("/mydir"):
if file.endswith(".txt"):
print(os.path.join("/mydir", file))

or if you want to traverse directory, use os.walk:

import os
for root, dirs, files in os.walk("/mydir"):
for file in files:
if file.endswith(".txt"):
print(os.path.join(root, file))

How to read all files in a folder using C

You can use this sample code and modify it if you need:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>

/* This is just a sample code, modify it to meet your need */
int main(int argc, char **argv)
{
DIR* FD;
struct dirent* in_file;
FILE *common_file;
FILE *entry_file;
char buffer[BUFSIZ];

/* Openiing common file for writing */
common_file = fopen(path_to_your_common_file, "w");
if (common_file == NULL)
{
fprintf(stderr, "Error : Failed to open common_file - %s\n", strerror(errno));

return 1;
}

/* Scanning the in directory */
if (NULL == (FD = opendir (in_dir)))
{
fprintf(stderr, "Error : Failed to open input directory - %s\n", strerror(errno));
fclose(common_file);

return 1;
}
while ((in_file = readdir(FD)))
{
/* On linux/Unix we don't want current and parent directories
* On windows machine too, thanks Greg Hewgill
*/
if (!strcmp (in_file->d_name, "."))
continue;
if (!strcmp (in_file->d_name, ".."))
continue;
/* Open directory entry file for common operation */
/* TODO : change permissions to meet your need! */
entry_file = fopen(in_file->d_name, "rw");
if (entry_file == NULL)
{
fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno));
fclose(common_file);

return 1;
}

/* Doing some struf with entry_file : */
/* For example use fgets */
while (fgets(buffer, BUFSIZ, entry_file) != NULL)
{
/* Use fprintf or fwrite to write some stuff into common_file*/
}

/* When you finish with the file, close it */
fclose(entry_file);
}

/* Don't forget to close common file before leaving */
fclose(common_file);

return 0;
}

Hope this hellp.

Regards.

Open all files in a folder using notepad++

You can just simply drag the folder containing your site files into notepad++. It opens all files(I assume it just opens txt,html, and other compatible files) including ones in sub directories.

Alternatively, if you want to do them one month at a time you could do File>Open and then shift+click from the top to the bottom to select as many files as you want.

Or just use a better text editor like sublime text where you can just open entire folders

Open all files in a folder

Sounds like you're on linux or some Unix variant.
Using the asterisk gets you all files in the current folder:

$ vim *


Related Topics



Leave a reply



Submit