我正在用python创建一个日历系统,该系统创建一个带有描述作为其内容的文件。该文件将被放置在目录中,文件的日期相同,以便检测重叠事件。
创建后,我曾尝试使用Shutil将文件移入,但是我没有运气,也没有将目录设置为新文件夹。 (为不好的编码而道歉,我是python的新手,还不太了解格式设置和解决这些逻辑问题的最佳方法)。
def newEvent(): # Creates new event
directory = input("Input the calender directory. This can be changed in the settings: ")
pattern = "^(3[01]|[12][0-9]|0[1-9]).(1[0-2]|0[1-9]).[0-9]{4}$" # DD.MM.YYY Pattern
eventName = input("Please enter the name of the event: ")
print("'" + eventName + "'" + " successfully Registered.")
time.sleep(0.5)
eventDate = input("Please enter the date of the event in the 'DD.MM.YYYY' format: ")
time.sleep(0.5)
if re.match(pattern, eventDate): # Matches pattern with eventDate
print(eventName + " successfully Registered on the " + eventDate)
time.sleep(0.5)
description = input("Please input a description for the event: ")
time.sleep(0.5)
if os.path.exists(re.sub('\.', '', eventDate)):
fileName = str(re.sub('\.', '', eventDate)) + "/" + eventDate + " - " + eventName + ".txt" # Creates the file name
newFile = open(str(fileName), "w+") # Creates the file
newFile.write(description) # Adds description
newFile.close()
else:
newDir = os.mkdir((re.sub('\.', '', eventDate))) # Creates new directory named the event date minus the "."
fileName = "/" + eventDate + " - " + eventName + ".txt"
newFile = open(str(directory) + (fileName), "w+")
newFile.write(description)
newFile.close()
shutil.move(str(newFile), str(newDir))
print("Your event: " + str(eventName) + " taking place on the " + eventDate + " has been registered. Returning to menu.")
time.sleep(0.5)
menu()
else:
print("Date not valid, returning to 'New Event'.")
time.sleep(0.5)
newEvent()
我希望代码将newFile移到创建的目录中,但是它将在.py文件的目录中创建文件。
答案 0 :(得分:0)
问题在于路径的定义。 另外,您还尝试移动newFile(它是一个TextWrapper实例),而不是路径字符串或类似路径的对象(但是您将其转换为字符串,因此终端不会给您正确的错误消息)
我建议您使用os.path.join创建路径:它更干净,更不易出错并且具有多平台功能,因此您不必担心Windows和linux路径分隔符(\或/)之间的冲突。
此外,您可以使用正确的绝对路径直接定义文件名,从而不必移动任何内容
def newEvent(): # Creates new event
directory = input("Input the calender directory. This can be changed in the settings: ")
pattern = "^(3[01]|[12][0-9]|0[1-9]).(1[0-2]|0[1-9]).[0-9]{4}$" # DD.MM.YYY Pattern
eventName = input("Please enter the name of the event: ")
print("'" + eventName + "'" + " successfully Registered.")
time.sleep(0.5)
eventDate = input("Please enter the date of the event in the 'DD.MM.YYYY' format: ")
time.sleep(0.5)
if re.match(pattern, eventDate): # Matches pattern with eventDate
print(eventName + " successfully Registered on the " + eventDate)
time.sleep(0.5)
description = input("Please input a description for the event: ")
time.sleep(0.5)
event_dir = os.path.join(directory, re.sub('\.', '', eventDate))
fileName = os.path.join(directory, event_dir, eventDate + " - " + eventName + ".txt") # Creates the file name
if os.path.exists(event_dir):
newFile = open(str(fileName), "w+") # Creates the file
newFile.write(description) # Adds description
newFile.close()
else:
newDir = os.mkdir(event_dir) # Creates new directory named the event date minus the "."
newFile = open(fileName, "w+")
newFile.write(description)
newFile.close()
# shutil.move(str(fileName), str(newDir))
print("Your event: " + str(eventName) + " taking place on the " + eventDate + " has been registered. Returning to menu.")
time.sleep(0.5)
menu()
else:
print("Date not valid, returning to 'New Event'.")
time.sleep(0.5)
newEvent()