-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
get_directory_file_info
for exploring a directory of files (#142
) * added python-pptx to requirements * added filetype detection for powerpoint * add more filetypes to detect * more tests * added tests for filetype * reorder document types * tests for get_directory_file_info * added docs for get_directory_file_info * bump version * Word -> Office * added test for filetype * add group by filetype example
- Loading branch information
1 parent
7e3af6c
commit eba4c80
Showing
11 changed files
with
313 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
|
||
import pandas as pd | ||
|
||
import unstructured.file_utils.exploration as exploration | ||
|
||
|
||
def test_get_directory_file_info(tmpdir): | ||
file_info_test = os.path.join(tmpdir, "file_info_test") | ||
if not os.path.exists(file_info_test): | ||
os.mkdir(file_info_test) | ||
|
||
directory1 = os.path.join(file_info_test, "directory1") | ||
if not os.path.exists(directory1): | ||
os.mkdir(directory1) | ||
|
||
filename1 = os.path.join(directory1, "filename1.txt") | ||
with open(filename1, "w") as f: | ||
f.write("hello there!") | ||
|
||
directory2 = os.path.join(file_info_test, "directory2") | ||
if not os.path.exists(directory2): | ||
os.mkdir(directory2) | ||
|
||
filename2 = os.path.join(directory2, "filename2.txt") | ||
with open(filename2, "w") as f: | ||
f.write("hello there!") | ||
|
||
file_info = exploration.get_directory_file_info(file_info_test) | ||
assert isinstance(file_info, pd.DataFrame) | ||
assert set(file_info["filename"].to_list()) == {"filename1.txt", "filename2.txt"} | ||
|
||
means = file_info.groupby("filetype").mean() | ||
assert means.columns.to_list() == ["filesize"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.4.0-dev0" # pragma: no cover | ||
__version__ = "0.4.0" # pragma: no cover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
from typing import Any, Dict, List | ||
|
||
import pandas as pd | ||
|
||
from unstructured.file_utils.filetype import detect_filetype | ||
|
||
|
||
def get_directory_file_info(directory: str) -> pd.DataFrame: | ||
"""Recursively walks a directory and extracts key file information to support initial | ||
exploration of text data sets. Returns a pandas DataFrame.""" | ||
data: Dict[str, List[Any]] = { | ||
"filename": [], | ||
"path": [], | ||
"filesize": [], | ||
"extension": [], | ||
"filetype": [], | ||
} | ||
for path, _, files in os.walk(directory): | ||
for filename_no_path in files: | ||
filename = os.path.join(path, filename_no_path) | ||
_, extension = os.path.splitext(filename) | ||
filesize = os.path.getsize(filename) | ||
filetype = detect_filetype(filename) | ||
|
||
data["filename"].append(filename_no_path) | ||
data["path"].append(path) | ||
data["extension"].append(extension) | ||
data["filesize"].append(filesize) | ||
data["filetype"].append(filetype) | ||
|
||
return pd.DataFrame(data) |
Oops, something went wrong.