我知道使用
getpass.getuser()命令,我可以获取用户名,但是如何自动在以下脚本中实现呢?所以我希望python找到用户名,然后在下面的脚本中实现它.
脚本: os.path.join('..','Documents and Settings','USERNAME','Desktop'))
(正在使用Python版本2.7)
在Firefox中,使用Imacros,我想从批处理文件中启动多个宏,但问题是:我希望它们逐个运行.
因此,首先"宏1"将在完成后运行,"宏2"将运行,依此类推,直到"宏7".
我的批次代码:
cd C:\Program Files\Mozilla Firefox
start firefox.exe
ping -n 05 127.0.0.1>nul
start firefox.exe imacros://run/?m=NAMEofMACRO.iim
Imacros VERSION BUILD=7601105
Run Code Online (Sandbox Code Playgroud) 我有一个数组,下面是重复的字符串.我想找到并替换这些字符串,但每次匹配时我都想更改替换字符串的值.
让我来证明一下.
此样本数组:
SampleArray = ['champ', 'king', 'king', 'mak', 'mak', 'mak']
Run Code Online (Sandbox Code Playgroud)
应该改为:
SampleArray = ['champ', 'king1', 'king2', 'mak1', 'mak2', 'mak3']
Run Code Online (Sandbox Code Playgroud)
如何使这成为可能?我已经在这3天了,没有运气.提前致谢.
My Failed Code:
import os, collections, re
SampleArray = ['champ', 'king', 'king', 'mak', 'mak', 'mak']
dupes = [x for x, y in collections.Counter(SampleArray).items() if y > 1]
length = len(dupes)
count = 0
while count < length:
j = 0
instances = SampleArray.count(dupes[count])
while j < instances:
re.sub(dupes[count], dupes[count] + j, SampleArray, j)
j += 1
count += 1 …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释我的代码有什么问题吗?我收到以下错误:
回溯(最近一次调用最后一次):
文件"C:\ LineRep.py",第15行,在模块中:
对于File2中的行:
ValueError:对已关闭文件的I/O操作
我的代码:
import os, Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
dirprompt = tkFileDialog.askopenfilename()
File = open (dirprompt, 'r')
File2 = open (dirprompt + 'temp', 'w')
for line in File:
File2.write(line.replace(',', ' '))
File.close()
File2.close()
names = []
for line in File2:
names.append(line)
print names
Run Code Online (Sandbox Code Playgroud) 在下面的Perl程序中,由于某种原因,整数除法不会发生.相反,控制台输出两个整数的连接,也不会发生divsion的if语句.为什么是这样?谢谢.
码:
print "Please Enter Your First Number\n";
$num1 = <>; chomp $num1;
print "Please Enter Your Operation\n";
$operation = <>; chomp $operation;
print "Please Enter Your Second Number\n";
$num2 = <>; chomp $num2;
if ($operation == "+"){
$result = $num1 + $num2;
} elsif ($operation == "-"){
$result = $num1 - $num2;
} elsif ($operation == "*"){
$result = $num1 * $num2;
#Problem HERE:
} elsif ($operation == "/"){
if ($num2 == 0){
print "Cant Divide be Zero Mate\n"; …Run Code Online (Sandbox Code Playgroud) 所以我试图解析一个巨大的文件,而下面的代码解析时间太长。文件大小为 2GB。我希望有人能帮助我加快速度。
import os, shlex
def extractLinkField(inDir, outDir):
fileList = os.listdir(inDir)
links = set()
for File in fileList:
print(File)
with open(os.path.join(inDir, File), encoding="utf8") as inFile:
for line in inFile:
try:
links.add(shlex.split(line)[2])
except Exception:
continue
outFile = open(os.path.join(outDir, 'extractedLinks.txt'), 'a+', encoding="utf8")
for link in list(links):
outFile.write(link + '\n')
outFile.close()
Path = os.path.join(os.getcwd(), 'logs')
extractLinkField(Path, os.getcwd())
Run Code Online (Sandbox Code Playgroud)
文件格式如下:
90 "m z pd gk y xr vo" "n l v ogtc dj wzb" "d zi pfgyo b tmhek" "df qu venr ls hzw j" …Run Code Online (Sandbox Code Playgroud) 我在这里有一个返回4位数字符串的函数.问题是,当我运行500次或更多次的函数时,它开始返回重复项.怎么避免呢?
我的职责:
import random
def CreatePass():
Num = str(random.randint(1000, 9999)
return Num
Run Code Online (Sandbox Code Playgroud)