Making the file read-only will not allow the file to be rewritten again. For this, we need to modify the permissions of the file. To achieve this, we will make use of the os module in Python more specifically, the chmod() of the os module.
The coding part is extremely simple and will contain very few lines as we are not doing much but changing the permissions. Using the chmod(), we can change the mode of the path, setting it to any mode using the suitable flags from the stat module. Both these modules come inbuilt with Python and hence you need not install anything additionally.
The entire code to change the file to read-only is as follows;
import os
from stat import S_IREAD
# Replace the first parameter with your file name
os.chmod("sample.txt", S_IREAD)
.
You can verify if the code was executed correctly by checking the file’s permissions. To do that :
Right-click on the file and click properties.
Under the attributes section, you will find the read-only checkbox checked.
I hope you found this article useful and it helped you make a file read-only. You can do more than just making the file read-only by using the appropriate flag from the stat module.
Answer:
The table data is displayed as it is when it is converted into the text. Only the borders of the table get vanished. The data is displayed in the form of rows and columns.
Explanation:
- Microsoft word has a feature that it converts the table into text and vice versa.
- This feature helps as sometimes the table does not attracts much as the text do.
- So for converting the table into text we take following steps:
- Select the entire table.
- Go to Layout tab.
- Locate the Data group.
- Click on "Convert to text"
The table will be converted to simple text.
I hope it will help you!
Answer:
public static ArrayList manyStrings(ArrayList<String> list, int n){
ArrayList<String> newList = new ArrayList<String>();
for (int i=0; i<list.size(); i++) {
for (int j=0; j<n; j++) {
newList.add(list.get(i));
}
}
return newList;
}
Explanation:
Create a method called manyStrings that takes two parameters, list and n
Create a new ArrayList that will hold new values
Create a nested for loop. The outer loop iterates through the list. The inner loop adds the elements, n of this element, to the newList.
When the loops are done, return the newList