如何使用Python 3搜索和替换文件中的文本?
这是我的代码:
import os
import sys
import fileinput
print ("Text to search for:")
textToSearch = input( "> " )
print ("Text to replace it with:")
textToReplace = input( "> " )
print ("File to perform Search-Replace on:")
fileToSearch = input( "> " )
#fileToSearch = 'D:\dummy1.txt'
tempFile = open( fileToSearch, 'r+' )
for line in fileinput.input( fileToSearch ):
if textToSearch in line :
print('Match Found')
else:
print('Match Not Found!!')
tempFile.write( line.replace( textToSearch, textToReplace ) )
tempFile.close()
input( '\n\n Press …Run Code Online (Sandbox Code Playgroud) 当我在IDLE中运行以下脚本时
import os
print(os.getcwd())
Run Code Online (Sandbox Code Playgroud)
我输出为
D:\testtool
Run Code Online (Sandbox Code Playgroud)
但是当我从cmd提示符运行时,我得到了
c:\Python33>python D:\testtool\current_dir.py
c:\Python33
Run Code Online (Sandbox Code Playgroud)
我如何获得使用IDLE获得的相同结果?
如何将空格分隔的整数输入转换为整数列表?
输入示例:
list1 = list(input("Enter the unfriendly numbers: "))
Run Code Online (Sandbox Code Playgroud)
转换示例:
['1', '2', '3', '4', '5'] to [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)