Home>Article>Backend Development> How to configure python environment in pycharm
How to configure python environment with pycharm: first specify the writable mode, the code is [f1.write('hello boy!')]; then close the relevant file to write the data in the cache to the file , the code is [[root@node1 ~]# hello boy!].
The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer.
How to configure python environment with pycharm:
Writing data directly is not possible because the 'r' read-only mode is opened by default
>>> f.write('hello boy') Traceback (most recent call last): File "", line 1, in IOError: File not open for writing >>> f
You should specify the writable mode first
>>> f1 = open('/tmp/test.txt','w') >>> f1.write('hello boy!')
But at this time the data is only written to the cache, not saved to the file, and as you can see from the output below, the original configuration has been cleared
[root@node1 ~]# cat /tmp/test.txt [root@node1 ~]#
Close this file to write the data in the cache to the file
>>> f1.close() [root@node1 ~]# cat /tmp/test.txt [root@node1 ~]# hello boy!
Note: This step needs to be very careful, because if the edited file exists, this step will clear it first. The file is rewritten. So what should you do if you don’t want to clear the file and then write it?
Using r mode will not clear it first, but will replace the original file, as in the following example: hello boy! is replaced by hello aay!
>>> f2 = open('/tmp/test.txt','r+') >>> f2.write('\nhello aa!') >>> f2.close() [root@node1 python]# cat /tmp/test.txt hello aay!
How to achieve no replacement?
>>> f2 = open('/tmp/test.txt','r+') >>> f2.read() 'hello girl!' >>> f2.write('\nhello boy!') >>> f2.close() [root@node1 python]# cat /tmp/test.txt hello girl! hello boy!
You can see that if you read the file before writing and then write, the written data will be added to the end of the file without replacing the original file. This is caused by pointers. The pointer in r mode is at the beginning of the file by default. If written directly, the source file will be overwritten. After reading the file through read(), the pointer will move to the end of the file and then write data. There will be no problem. A mode can also be used here
>>> f = open('/tmp/test.txt','a') >>> f.write('\nhello man!') >>> f.close() >>> [root@node1 python]# cat /tmp/test.txt hello girl! hello boy! hello man!
For introduction to other modes, see the table below:
Related free learning recommendations:python Video tutorial
The above is the detailed content of How to configure python environment in pycharm. For more information, please follow other related articles on the PHP Chinese website!