fileinteract 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fileinteract/__init__.py
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
import os
|
2
|
+
import shutil
|
3
|
+
|
4
|
+
def Exists(Path: str) -> bool:
|
5
|
+
return os.path.exists(Path)
|
6
|
+
|
7
|
+
class File:
|
8
|
+
def __init__(self, Path: str):
|
9
|
+
self.Path = Path
|
10
|
+
|
11
|
+
if not os.path.exists(self.Path):
|
12
|
+
raise FileNotFoundError()
|
13
|
+
|
14
|
+
self.Name = os.path.basename(self.Path)
|
15
|
+
self.Extension = os.path.splitext(self.Path)[1]
|
16
|
+
|
17
|
+
def GetContent(self, Mode: str = "normal", Encoding: str = "utf-8") -> bytes | str:
|
18
|
+
if not Exists(self.Path):
|
19
|
+
raise FileNotFoundError()
|
20
|
+
|
21
|
+
if Mode.lower() == "normal":
|
22
|
+
Mode = "r"
|
23
|
+
elif Mode.lower() == "binary":
|
24
|
+
Mode = "rb"
|
25
|
+
else:
|
26
|
+
raise "Invalid argument : Mode arg must be 'normal" or 'binary'
|
27
|
+
|
28
|
+
with open(self.Path, Mode, encoding=Encoding if Mode == "r" else None) as F:
|
29
|
+
return F.read()
|
30
|
+
|
31
|
+
def OverWrite(self, Content: str | bytes, Encoding: str = "utf-8") -> None:
|
32
|
+
if not Exists(self.Path):
|
33
|
+
raise FileNotFoundError()
|
34
|
+
|
35
|
+
with open(self.Path, "wb" if str(type(Content)) == "<class 'bytes'>" else "w", encoding=Encoding if str(type(Content)) == "<class 'str'>" else None) as F:
|
36
|
+
F.write(Content)
|
37
|
+
|
38
|
+
def Write(self, Content: str | bytes, Encoding: str = "utf-8") -> None:
|
39
|
+
if not Exists(self.Path):
|
40
|
+
raise FileNotFoundError()
|
41
|
+
|
42
|
+
with open(self.Path, "ab" if str(type(Content)) == "<class 'bytes'>" else "a", encoding=Encoding if str(type(Content)) == "<class 'str'>" else None) as F:
|
43
|
+
F.write(Content)
|
44
|
+
|
45
|
+
def Delete(self) -> None:
|
46
|
+
if not Exists(self.Path):
|
47
|
+
raise FileNotFoundError()
|
48
|
+
|
49
|
+
os.remove(self.Path)
|
50
|
+
|
51
|
+
def Copy(self, DistPath: str) -> None:
|
52
|
+
if not Exists(self.Path):
|
53
|
+
raise FileNotFoundError()
|
54
|
+
|
55
|
+
shutil.copy(self.Path, DistPath)
|
56
|
+
|
57
|
+
def ClearContent(self):
|
58
|
+
if not Exists(self.Path):
|
59
|
+
raise FileNotFoundError()
|
60
|
+
|
61
|
+
with open(self.Path, "w", encoding='utf-8') as F:
|
62
|
+
F.write('')
|
63
|
+
|
64
|
+
def Move(self, DistPath: str) -> None:
|
65
|
+
if not Exists(self.Path):
|
66
|
+
raise FileNotFoundError()
|
67
|
+
|
68
|
+
shutil.copy(self.Path, DistPath)
|
69
|
+
os.remove(self.Path)
|
70
|
+
|
71
|
+
def GetContentLines(self, Encoding: str = 'utf-8') -> list[str]:
|
72
|
+
if not Exists(self.Path):
|
73
|
+
raise FileNotFoundError()
|
74
|
+
|
75
|
+
with open(self.Path, "r", encoding=Encoding) as F:
|
76
|
+
return F.readlines()
|
77
|
+
|
78
|
+
def GetLine(self, Index: int, Encoding: str = "utf-8") -> str | None:
|
79
|
+
if not Exists(self.Path):
|
80
|
+
raise FileNotFoundError()
|
81
|
+
|
82
|
+
with open(self.Path, "r", encoding=Encoding) as F:
|
83
|
+
ContentLines = F.readlines()
|
84
|
+
|
85
|
+
try:
|
86
|
+
return ContentLines[Index]
|
87
|
+
except IndexError:
|
88
|
+
return None
|
89
|
+
|
90
|
+
def GetCharsCount(self, Encoding: str = "utf-8") -> int:
|
91
|
+
if not Exists(self.Path):
|
92
|
+
raise FileNotFoundError()
|
93
|
+
|
94
|
+
with open(self.Path, "r", encoding=Encoding) as F:
|
95
|
+
return len(F.read())
|
96
|
+
|
97
|
+
def GetLinesCount(self, Encoding: str = "utf-8") -> int:
|
98
|
+
if not Exists(self.Path):
|
99
|
+
raise FileNotFoundError()
|
100
|
+
|
101
|
+
with open(self.Path, "r", encoding=Encoding) as F:
|
102
|
+
return len(F.readlines())
|
103
|
+
|
104
|
+
def GetSize(self) -> int:
|
105
|
+
if not Exists(self.Path):
|
106
|
+
raise FileNotFoundError()
|
107
|
+
|
108
|
+
return os.path.getsize(self.Path)
|
109
|
+
|
110
|
+
def Rename(self, NewName: str) -> None:
|
111
|
+
if not Exists(self.Path):
|
112
|
+
raise FileNotFoundError()
|
113
|
+
|
114
|
+
os.rename(self.Path, os.path.join(self.Path.replace(self.Name, ""), NewName))
|
115
|
+
|
116
|
+
self.Path = self.Path.replace(self.Name, NewName)
|
117
|
+
self.Name = NewName
|
118
|
+
self.Extension = os.path.splitext(self.Path)[1]
|
119
|
+
|
120
|
+
def IsBinary(self) -> bool:
|
121
|
+
if not Exists(self.Path):
|
122
|
+
raise FileNotFoundError()
|
123
|
+
|
124
|
+
with open(self.Path, "rb") as F:
|
125
|
+
Chunk = F.read(1024)
|
126
|
+
|
127
|
+
if b'\x00' in Chunk:
|
128
|
+
return True
|
129
|
+
|
130
|
+
text_chars = bytes(range(32, 127)) + b'\n\r\t\b'
|
131
|
+
NonStr = sum(Byte not in text_chars for Byte in Chunk)
|
132
|
+
|
133
|
+
return NonStr / len(Chunk) > 0.30 if Chunk else False
|
134
|
+
|
135
|
+
def InsertLine(self, Content: str, Index: int, Encoding: str = 'utf-8') -> None:
|
136
|
+
if not Exists(self.Path):
|
137
|
+
raise FileNotFoundError()
|
138
|
+
|
139
|
+
Lines = self.GetContentLines()
|
140
|
+
|
141
|
+
try:
|
142
|
+
Lines.insert(Index, Content)
|
143
|
+
except IndexError:
|
144
|
+
raise IndexError()
|
145
|
+
|
146
|
+
with open(self.Path, "w", encoding=Encoding) as F:
|
147
|
+
F.write(Lines)
|
148
|
+
|
149
|
+
def Replace(self, WhatToReplace: str, ReplaceBy: str, Encoding: str) -> None:
|
150
|
+
if not Exists(self.Path):
|
151
|
+
raise FileNotFoundError()
|
152
|
+
|
153
|
+
FContent = self.GetContent()
|
154
|
+
|
155
|
+
with open(self.Path, "w", encoding=Encoding) as F:
|
156
|
+
F.write(FContent.replace(WhatToReplace, ReplaceBy))
|
157
|
+
|
158
|
+
def Contain(self, Search: str, Encoding: str = 'utf-8') -> bool:
|
159
|
+
if not Exists(self.Path):
|
160
|
+
raise FileNotFoundError()
|
161
|
+
|
162
|
+
with open(self.Path, "r", encoding=Encoding) as F:
|
163
|
+
return True if Search in F.read() else False
|
164
|
+
|
165
|
+
def IsEmpty(self) -> bool:
|
166
|
+
if not Exists(self.Path):
|
167
|
+
raise FileNotFoundError()
|
168
|
+
|
169
|
+
return False if len(self.GetSize()) > 0 else True
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: fileinteract
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: A simple lib to interact with files in Python
|
5
|
+
Author-email: Guillaume Plagier <ranswnry@gmail.com>
|
6
|
+
License-Expression: MIT
|
7
|
+
Requires-Python: >=3.9
|
8
|
+
Description-Content-Type: text/markdown
|
9
|
+
License-File: LICENSE
|
10
|
+
Dynamic: license-file
|
@@ -0,0 +1,6 @@
|
|
1
|
+
fileinteract/__init__.py,sha256=-DoAu0HqO1P4WYq-1I6Gq1SsmMU3IihR1FNJjFtEdF8,5687
|
2
|
+
fileinteract-0.1.0.dist-info/licenses/LICENSE,sha256=Gm9rRWPU7QZqyltsbuqgidGYSe5iChAYRmQni2unDsg,1115
|
3
|
+
fileinteract-0.1.0.dist-info/METADATA,sha256=BuAMIqgs1cKRh7WioahyTCeuTN9XRxvtf4kWu5Ku5lA,305
|
4
|
+
fileinteract-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
5
|
+
fileinteract-0.1.0.dist-info/top_level.txt,sha256=XOKqT2GR0geaqmbANterRXHW8Z_UB3qbLiJXdX72gxE,13
|
6
|
+
fileinteract-0.1.0.dist-info/RECORD,,
|
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Guillaume Plagier
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1 @@
|
|
1
|
+
fileinteract
|