Skip to content

Commit 270d807

Browse files
committed
Fixed #1,#2; Update file:(new property<full_name>,property<name>,property<ext> of class<File>),doc
1 parent e8d635d commit 270d807

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

doc/API reference.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
### Constructor
44

55
```python
6-
File(filePath)
6+
File(file_path)
77
```
88

99
| Parameter | Type | Default | Description |
1010
| --------- | ------ | ------- | ------------------------------------------------------------ |
11-
| filePath | string | | The absolute path or relative path of the file. If using a relative path, it must include `.` or `/`. |
11+
| file_path | string | | The absolute path or relative path of the file. If using a relative path, it must include `.` or `/`. |
1212

1313
### Attributes
1414

@@ -17,18 +17,31 @@ File(filePath)
1717
<td><code>File.content</code></td>
1818
<td>Return the entire content of the file.</td>
1919
</tr>
20+
<tr>
21+
<td><code>File.full_name</code></td>
22+
<td>Return the name of the file.</td>
23+
</tr>
24+
<tr>
25+
<td><code>File.name</code></td>
26+
<td>Return the name of the file without the file extension.</td>
27+
</tr>
28+
<tr>
29+
<td><code>File.ext</code></td>
30+
<td>Return the file extension of the file.</td>
31+
</tr>
2032
</table>
2133

34+
2235
### Operation
2336

24-
#### File.Rewrite(content)
37+
#### File.rewrite(content)
2538

2639
Rewrite the entire content of the file.
2740

28-
#### File.Append(content)
41+
#### File.append(content)
2942

3043
Append content at the end of file content.
3144

32-
#### File.Delete()
45+
#### File.delete()
3346

3447
Delete file.

easierfile/file.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,50 @@
11
import os
22

33
class File:
4-
def __init__(self, filePath):
5-
self.__m_filePath = filePath
6-
self.__m_dirPath = os.path.dirname(self.__m_filePath)
7-
if os.path.exists(self.__m_dirPath) == False: # If the directory of the file path does not exist, it will be created.
8-
os.mkdir(self.__m_dirPath)
4+
def __init__(self, file_path):
5+
self.__m_file_path = file_path
6+
self.__m_file_full_name = os.path.basename(self.__m_file_path)
7+
self.__m_file_name, self.__m_file_extension = os.path.splitext(self.__m_file_full_name)
8+
self.__m_dir_path = os.path.dirname(self.__m_file_path)
9+
10+
if os.path.exists(self.__m_dir_path) == False: # If the directory of the file path does not exist, it will be created.
11+
os.mkdir(self.__m_dir_path)
912
try: # If the file does not exist, it will be created.
10-
self.__m_file = open(self.__m_filePath, "x")
11-
except:
12-
pass
13+
self.__m_file = open(self.__m_file_path, "x")
14+
except FileExistsError:
15+
pass # Indicates that the file exists and no further exception handling is required.
1316
else:
1417
self.__m_file.close()
1518

1619
@property
1720
def content(self):
18-
self.__m_file = open(self.__m_filePath, "r")
21+
self.__m_file = open(self.__m_file_path, "r")
1922
self.__m_content = self.__m_file.read()
2023
self.__m_file.close()
2124
return self.__m_content
2225

23-
def Rewrite(self,content):
24-
self.__m_file = open(self.__m_filePath, "w")
26+
@property
27+
def full_name(self):
28+
return self.__m_file_full_name
29+
30+
@property
31+
def name(self):
32+
return self.__m_file_name
33+
34+
@property
35+
def ext(self):
36+
return self.__m_file_extension.split(".")[-1]
37+
38+
def rewrite(self,content):
39+
self.__m_file = open(self.__m_file_path, "w")
2540
self.__m_file.write(content)
2641
self.__m_file.close()
2742

28-
def Append(self,content):
29-
self.__m_file = open(self.__m_filePath, "a")
43+
def append(self,content):
44+
self.__m_file = open(self.__m_file_path, "a")
3045
self.__m_file.write(content)
3146
self.__m_file.close()
3247

33-
def Delete(self):
34-
if os.path.exists(self.__m_filePath):
35-
os.remove(self.__m_filePath)
48+
def delete(self):
49+
if os.path.exists(self.__m_file_path):
50+
os.remove(self.__m_file_path)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "easierfile"
3-
version = "1.0.1"
3+
version = "1.1.0"
44
description = "A simple Python package that object-oriented encapsulates Python traditional built-in file operations."
55
license = "MIT"
66
authors = ["leoweyr <[email protected]>"]

0 commit comments

Comments
 (0)