Files in Python:
Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.
Given a file, the task is to write 1 to 100 numbers into a given file in Python.
- Python Program to Delete Specific Line from a Text File
- Python Program to Count Words in a Text File those are Ending with Alphabet “e”
- Python Program to Count Number of Digits in a Text File
Program to Write 1 to 100 in a Text File in Python
Below is the full approach for writing 1 to 100 numbers into a given file in Python.
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
-
Open the file in write mode. In this case, we’re writing the contents into the file.
-
Iterate from 1 to 100 using the for loop.
-
Convert the iterator value into a string using the str() function and write it into the file using the write() function.
-
The Exit of Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in write mode. In this case, we're writing the contents into the file. with open(givenFilename, 'w') as givenfilecontent: # Iterate from 1 to 100 using the for loop for itr in range(1,101): # Convert the iterator value into a string using the str() function and write it into # the file using the write() function givenfilecontent.write(str(itr) + "\t")
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Explanation:
- The file path is stored in the variable ‘file name.’ Change the value of this variable to the path of your own file.
- Dragging and dropping a file onto the terminal will show its path. The code will not run unless you change the value of this variable.
- The file will be opened in writing mode. Use the open() function to open a file. The path to the file is the method’s first parameter, and the mode to open the file is the method’s second parameter.
- When we open the file, we use the character ‘w’ to signify write-mode.
- We write the data to file using the write() function.
File Content Before Writing:
hello this is btechgeeks sample file specific python codes Summary
File Content After Writing 1 to 100:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Note:
It erases all the previous file content and overwrites into it
Google Colab Images:
Files and Code: