Home > Backend Development > Python Tutorial > How to configure python environment in pycharm

How to configure python environment in pycharm

coldplay.xixi
Release: 2023-01-03 09:27:18
Original
44562 people have browsed it

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!].

How to configure python environment in pycharm

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 "<stdin>", line 1, in <module>
IOError: File not open for writing
>>> f
<open file &#39;/tmp/test.txt&#39;, mode &#39;r&#39; at 0x7fe550a49d20>
Copy after login

You should specify the writable mode first

>>> f1 = open(&#39;/tmp/test.txt&#39;,&#39;w&#39;)
>>> f1.write(&#39;hello boy!&#39;)
Copy after login

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 ~]#
Copy after login

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!
Copy after login

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(&#39;/tmp/test.txt&#39;,&#39;r+&#39;)
>>> f2.write(&#39;\nhello aa!&#39;)
>>> f2.close()
[root@node1 python]# cat /tmp/test.txt
hello aay!
Copy after login

How to achieve no replacement?

>>> f2 = open(&#39;/tmp/test.txt&#39;,&#39;r+&#39;)
>>> f2.read()
&#39;hello girl!&#39;
>>> f2.write(&#39;\nhello boy!&#39;)
>>> f2.close()
[root@node1 python]# cat /tmp/test.txt
hello girl!
hello boy!
Copy after login

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(&#39;/tmp/test.txt&#39;,&#39;a&#39;)
>>> f.write(&#39;\nhello man!&#39;)
>>> f.close()
>>>
[root@node1 python]# cat /tmp/test.txt
hello girl!
hello boy!
hello man!
Copy after login

For introduction to other modes, see the table below:

How to configure python environment in pycharm

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template