File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Text File Analyzer
2
+
3
+ This Python script reads a text file and provides a basic analysis by counting the number of lines, words, and characters in the file.
4
+
5
+ ## Usage
6
+
7
+ 1 . Make sure you have Python installed on your system.
8
+
9
+ 2 . Save the script as ` text_file_analyzer.py ` in your desired directory.
10
+
11
+ 3 . Run the script using a terminal or command prompt.
12
+
13
+ 4 . The script will prompt you to enter the path of the text file you want to analyze.
14
+
15
+ 5 . After entering the file path, the script will display the following information:
16
+ - Number of lines in the file
17
+ - Number of words in the file
18
+ - Number of characters in the file
19
+
20
+ ## Example
21
+
22
+ Suppose you have a text file named ` sample.txt ` with the following content:
23
+
24
+ This is a sample text file.
25
+ It contains multiple lines of text.
26
+ Let's see how the analyzer handles it.
27
+
28
+ Running the script and providing the path to ` sample.txt ` would output:
29
+ Number of lines: 3
30
+ Number of words: 17
31
+ Number of characters: 96
32
+
33
+ ## Note
34
+
35
+ This script provides a basic analysis for simple text files. It may not handle complex file formats or special cases.
Original file line number Diff line number Diff line change
1
+ def text_file_analyzer (file_path ):
2
+ try :
3
+ with open (file_path , 'r' ) as file :
4
+ content = file .read ()
5
+
6
+ # Count lines
7
+ num_lines = content .count ('\n ' ) + 1
8
+
9
+ # Count words
10
+ words = content .split ()
11
+ num_words = len (words )
12
+
13
+ # Count characters
14
+ num_characters = len (content )
15
+
16
+ return num_lines , num_words , num_characters
17
+ except FileNotFoundError :
18
+ print (f"Error: File '{ file_path } ' not found." )
19
+ return 0 , 0 , 0
20
+
21
+ if __name__ == "__main__" :
22
+ file_path = input ("Enter the path of the text file: " )
23
+
24
+ lines , words , characters = text_file_analyzer (file_path )
25
+ print (f"Number of lines: { lines } " )
26
+ print (f"Number of words: { words } " )
27
+ print (f"Number of characters: { characters } " )
Original file line number Diff line number Diff line change
1
+ hello world its amazing.
You can’t perform that action at this time.
0 commit comments