我正在玩一款名为Hearthstone的交易卡游戏,由暴风雪制作。游戏相当不错,但缺乏基本功能,任何自称“竞争”的游戏都应具备,比如统计跟踪和重播。 正如我在标题中所说,我正在尝试创建一个(非常粗糙且做得不好)的脚本,让我记录我玩的每场比赛。由于我缺乏编程技巧,80%的脚本只是我从各种各样的地方借来的一堆代码,并且适合于让它做我想做的事情。 我们的想法是让它像这样工作:
我拍摄了每一个回合的照片。它可能会变得烦人,但我不敢考虑实施OCR,以使脚本在每个转弯开始时自己拍照。会很棒但是我不能这样做......
游戏将每张图片发送到桌面(无需编写代码)。
在游戏结束时我运行脚本
2.1每个匹配都会有一个带编号的文件夹,因此脚本会创建该文件夹。这些文件夹将被称为“Match1”,“Match2”等。你可以看到写得多么糟糕,因为我是靠自己制作的:P
import sys
import os
import shutil
def checkFolder():
os.path.join('D:\Hearthstone\Replays\Match1')
matchNumber=1
while os.path.exists("D:\\Hearthstone\\Replays\\Match"+ str(matchNumber)) is True:
matchNumber=matchNumber + 1
else:
os.makedirs("D:\Hearthstone\Replays\Match"+str(matchNumber))
2.2 脚本将照片从桌面发送到最近创建的文件夹。问题是我不知道如何使脚本将目标文件夹更改为创建的最新文件夹。 我没有写这部分代码,我只是改编了它。资料来源:http://tinyurl.com/srcbh
folder = os.path.join('C:\\Users\\Felipe\\', 'Desktop') # Folder in which the images are in.
destination = os.path.join('D:\\Hearthstone\\Replays\\', 'match9999') #**Destination needs to be the newest folder and I dont know how to implement that...
extmove = 'png' # The extension you wish to organize.
num = 0 # Variable simply to use after to count images.
for filename in os.listdir(folder): #Run through folder.
extension = filename.split(".")[-1] # This strips the extensions ready to check and places into the extension
if extension == extmove: # If statement. If the extension of the file matches the one set previously then..
shutil.move(folder + "\\" + filename, destination) # Move the file from the folder to the destination folder. Also previously set.
num = num + 1
print(num)
print (filename, extension)
就是这样! 我需要步骤2.2的帮助。我当然感谢您的帮助! 现在,我之所以发这么大的原因,是因为我想揭露我的想法,并希望激励某人认真对待类似的项目。 Hearthstone有成千上万的玩家可以从中受益,更不用说对于有更多经验的人来说,这似乎是一项相当容易的任务。
答案 0 :(得分:1)
好的,我终于开始工作了!
import sys
import os
import shutil
def sendPhotos():
matchNumber=1
photos_dest = "D:\\Hearthstone\\Replays\\Match"
while os.path.exists(photos_dest+ str(matchNumber)): #creates the name of the folder "Match1", "Match2", etc.
matchNumber=matchNumber + 1
else:
photos_destination = photos_dest+str(matchNumber)
os.makedirs(photos_destination)
for files in os.listdir('C:\\Users\\Felipe\\Desktop'):#only png files are moved
if files.endswith(".png"):
shutil.move(files, photos_destination)
sendPhotos()
感谢那些给我一些答案的人!我真的很感激!
答案 1 :(得分:0)
这会查看文件夹中的匹配项,并获取该文件夹的最小编号。
folder = os.path.join('C:\\Users\\Felipe\\', 'Desktop') # Folder in which the images are in.
recorded_matches_location = 'D:\\Hearthstone\\Replays\\'
match_number = 1
match_name = 'match1'
while match_name in os.listdir(recorded_matches_location):
match_number = 1 + match_number
match_name = 'match' + str(match_number) # corrected it! there must be a string and not a variable
destination = os.path.join(recorded_matches_location, match_name) #**Destination needs to be the newest folder and I dont know how to implement that...
extmove = 'png' # The extension you wish to organize.
num = 0 # Variable simply to use after to count images.
for filename in os.listdir(folder): #Run through folder.
extension = filename.split(".")[-1] # This strips the extensions ready to check and places into the extension
if extension == extmove: # If statement. If the extension of the file matches the one set previously then..
shutil.move(folder + "\\" + filename, destination) # Move the file from the folder to the destination folder. Also previously set.
num = num + 1
print(num)
print (filename, extension)
答案 2 :(得分:0)
嗯,首先,您确定问题并整理解决方案的事实表明您当然不缺乏编程技能。给自己一点功劳。你只需要更多练习。 :)
这应该更好,我没有运行它,所以可能会有一些错误:P
def checkFunction(base_dir='D:\\Hearthstone\\Replays\\'): #set this as a parameter with a default
match_number = 1
if os.path.exists(base_dir): #if the base directory doesn't exist you'll have a bad time
while os.path.exists(os.path.join(base_dir, 'Match{0}'.format(match_number)))
match_number += 1
new_dir = os.path.join(base_dir, 'Match{0}'.format(match_number))
os.makedirs(new_dir)
return new_dir
对于函数checkFolder,我建议让它返回新的目录名(如上所示)。你还需要缩进它下面的所有行,这样python知道那些行是该函数的一部分(虽然这可能仅仅是SO上的格式化问题)。
然后,一旦checkFolder功能正常工作,你在2.2中所做的更改就是:
destination = checkFolder()