pycharm配置Python環境 pythoncharm環境變量設置



文章插圖
pycharm配置Python環境 pythoncharm環境變量設置

文章插圖
【pycharm配置Python環境 pythoncharm環境變量設置】pycharm配置python環境的方法:直接的寫入數據是不行的,因為默認打開的是’r’ 只讀模式>>> f.write(‘hello boy’)Traceback (most recent call last):File “”, line 1, inIOError: File not open for writing>>> f應該先指定可寫的模式>>> f1 = open(‘/tmp/test.txt’,’w’)>>> f1.write(‘hello boy!’)但此時數據只寫到了緩存中,并未保存到文件,而且從下面的輸出可以看到,原先里面的配置被清空了[[email protected] ~]# cat /tmp/test.txt[[email protected] ~]#關閉這個文件即可將緩存中的數據寫入到文件中>>> f1.close()[[email protected] ~]# cat /tmp/test.txt[[email protected] ~]# hello boy!注意:這一步需要相當慎重,因為如果編輯的文件存在的話,這一步操作會先清空這個文件再重新寫入 。那么如果不要清空文件再寫入該如何做呢?使用r 模式不會先清空,但是會替換掉原先的文件,如下面的例子:hello boy! 被替換成hello aay!>>> f2 = open(‘/tmp/test.txt’,’r ‘)>>> f2.write(‘\nhello aa!’)>>> f2.close()[[email protected] python]# cat /tmp/test.txthello aay!如何實現不替換?>>> f2 = open(‘/tmp/test.txt’,’r ‘)>>> f2.read()‘hello girl!’>>> f2.write(‘\nhello boy!’)>>> f2.close()[[email protected] python]# cat /tmp/test.txthello girl!hello boy!可以看到,如果在寫之前先讀取一下文件,再進行寫入,則寫入的數據會添加到文件末尾而不會替換掉原先的文件 。這是因為指針引起的,r 模式的指針默認是在文件的開頭,如果直接寫入,則會覆蓋源文件,通過read() 讀取文件后,指針會移到文件的末尾,再寫入數據就不會有問題了 。這里也可以使用a 模式>>> f = open(‘/tmp/test.txt’,’a’)>>> f.write(‘\nhello man!’)>>> f.close()>>>[[email protected] python]# cat /tmp/test.txthello girl!hello boy!hello man!