0

第一次在这里发帖。我已经到处搜索以找出一种方法来做到这一点,但我要么不明白如何将现有答案应用于我的代码。

我想要做的是:我想接受用户输入(一年),确保它在几年之间,然后如果是的话,将它与现有字符串连接起来作为变量。

这段代码的结果是我通过并提供了所有必要的输入,然后它失败了 "fullInstr = str("cp -r /mnt/data/archive"+ fquery+ "/" + yquery+mquery+dquery+" /"+hquery+"*" + " " + outl2) TypeError: cannot concatenate 'str' and 'int' objects"

import os
import sys

os.system("clear")
overW = str("0")

outf = str("/autocopy.sh") # Output file full path and file name
if os.path.isfile("/autocopy.sh"): #
 overW = raw_input("This will overwrite the previous version. Do you want to continue? (y/n) ")
 os.system("clear")
else:
 os.system("clear")

if overW != "y":
 os.system("clear")
 sys.exit("No changes made.\n\n")
else:
 os.system("clear")
  #! Could also prompt for output file name, depending on how army-proof this needs to be.

finishMessage = "Finished."

outl = str("0") # Copy-to location
outl2 = str("0") # Modified Copy-to location
fquery = str("0") # A or B location variable
yquery = int("0") # Year variable
mquery = int("0") # Month variable
dquery = int("0") # Day variable
hquery = str("0") # Hour variable
mh1 = int("0") # Modified starting hour after transformation
mh2 = int("0") # Modified ending hour after transformation
mpath = str("0") # makes path if needed
fullInstr = str("0") # Full command set to write to file
formatList = (['A', 'B'])
yearList = list(range(2000,2099))
#monthList = (['01']-['12'])
 #! hquery is going to have to parse for ranges

# Instruction header
print "Builds a script to automatically copy folders and files from the storage array to a location of your choosing.\n"
print "Valid inputs for the questions below are numeric."
print "Year would be the full year, i.e. 2013"
print "Month is the two-digit month, i.e. 10"
print "Day is the two-digit day, i.e. 22"
print "Hour or hour range is just the first two digits of the hours you want to copy. i.e. 15 or 00-23\n\n"

outl = raw_input("Where do you want to copy the files to? Type the full path: ")
while not os.path.exists(outl):
 mpath = raw_input ("\nThat path doesn't exist on this system. Do you want to create it? (y/n) ")
 if mpath != "y":
  outl = raw_input("Where do you want to copy the files to? Type a valid path: ")
 else:
  os.mkdir(outl)
  print "\n"

if not outl.endswith("/"):
 outl2 = outl + "/"

fquery = raw_input("Do you want to copy A or B? ")
while not(fquery in formatList):
 print "\nInvalid input. You have to choose one of the two as printed above."
 fquery = raw_input("\nDo you want to copy A or B? ")
 print "\n"

yquery = int(raw_input("What year? "))
while yquery not in yearList:
 print "\nInvalid input. You have to choose a year in this century."
 yquery = int(raw_input("\nWhat year? "))
 print "\n"

mquery = raw_input("What day? ")
 #! Valid months are 01 to 12 
dquery = raw_input("What day? ")
 #! Valid days are 01 to 31
hquery = raw_input("What hour or hour range? ")
 #! if input is greater than two characters is must contain a - character
 #! if is not 00-23, separate first and last numbers and write a line for each. If it isn't a range, don't transform it.

#os.system("touch " + outf)
 #! if no error keep going

fullInstr = str("cp -r /mnt/data/archive"+ fquery+ "/" + yquery+mquery+dquery+"/"+hquery+"*" + " " + outl2)

os.system("echo "+fullInstr+ "> /autocopy.sh")


#os.system("chmod u+x "+outf) # Makes the output file executable
 #! if error = 0 set finishMessage to print "Your shell script is complete and is ready to run. Run it by typing ." + outf
 #! if error is <> 0 set finishMessage to print "Wasn't able to make the output file executable automatically. Use chmod to modify the file permissions manually on the file "+outf

#os.system("clear")

print finishMessage+"\n\n"

被注释掉的东西要么不起作用,要么尚未实施。我知道代码质量可能不是最好的,但这是我第一次编写代码来做我需要的事情。我已经尝试了很多东西,比如 yquery = str(yquery) 或者只是将 fullInstr 字符串更改为包含 str(yquery) ,但我无法让它工作。我变得沮丧。

4

2 回答 2

4

您应该使用format字符串格式化来避免担心被连接的变量的类型。

fullInstr = "cp -r /mnt/data/archive/{0}/{1}{2}{3}/{4}* {5}".format(fquery,yquery,mquery,dquery,hquery,out12)
于 2013-10-24T09:43:59.643 回答
3

那是因为您需要将 转换intstr对象。

    fullInstr = str("cp -r /mnt/data/archive" + fquery + "/" + str(yquery) + mquery + dquery + "/" + hquery + "*" + " " +                                      ^
                    outl2)                                      ^ 
#                                                               ^ -> Use str to convert

上面的代码在我的机器上工作。运行 Windows 8。Python 2.7.5。我只需要更改clearcls.

于 2013-10-24T09:31:34.597 回答