简体   繁体   English

Python的问题:打开应用程序/保存文件

[英]Trouble with Python: Opening an app/saving a file

I am a bit new to coding, I am trying to improve my skills. 我对编码有点陌生,我正在努力提高自己的技能。 My preferred programming language is python. 我首选的编程语言是python。 But I do need help with this bit of code. 但是我确实需要一些代码帮助。 I am making an app called User Helper for my own use. 我正在制作一个名为User Helper的应用程序供我自己使用。 I know that the code may look a bit sloppy, but when I posted it in for some reason I put four spaces in but it didn't turn to a block of code, so I had to manually put in the spaces. 我知道代码看起来可能有点草率,但是当我出于某种原因将其放进去时,我在其中放了四个空格,但是它没有变成一段代码,因此我不得不手动放置这些空格。

But back to the topic at hand, my code has 2 problems (though you could probably find more.) I am not able to save my favorites to a document for later use when I open the app again. 但是回到当前的主题,我的代码有两个问题(尽管您可能会发现更多问题。)当我再次打开应用程序时,我无法将收藏夹保存到文档中以供以后使用。 As well I don't know the code to open an app from python code. 同样,我不知道从python代码打开应用程序的代码。

I have tried many different methods including using the os function. 我尝试了许多不同的方法,包括使用os函数。 I think I may need some sort of directory, but I'm sorry to say I am unfamiliar with directories and I have had trouble with them. 我想我可能需要某种目录,但是很遗憾地说我不熟悉目录,但遇到了麻烦。 The time function will be used later in the project. 时间功能将在项目的后面使用。 And for some reason when I run this program, it tries to find a path from the folder all my coding projects are in. 出于某种原因,当我运行该程序时,它会尝试从我所有编码项目所在的文件夹中查找路径。

I also want to thank you for reading this, and I hope I can get this solved soon. 我还要感谢您阅读本文,希望我能尽快解决。 I plan to become an active member in this community, and I can't wait to continue improving my skills in coding. 我计划成为该社区的积极成员,迫不及待想继续提高自己的编码技能。

Thank you for your time! 感谢您的时间!

import os
import time

def name():
  print ""
  print ""
  print "Welcome to User Helper!"
  name1 = raw_input("What is your name?  ")
  print ""
  print "Welcom to User helper "+name1+"!"

def favorites():
  print ""
  print "Now opening Favorites settings..."
  fav1 = raw_input("Favorite 1: ")
  fav2 = raw_input("Favorite 2: ")
  fav3 = raw_input("Favorite 3: ")
  fav1full = fav1+".app"
  fav2full = fav2+".app"
  fav3full = fav3+".app"
  fav1_path = os.path.abspath(fav1full)
  fav2_path = os.path.abspath(fav2full)
  fav3_path = os.path.abspath(fav3full)
  os.system(fav1_path)

name()
favorites()

You can use os.chdir() to change default directory and os.startfile() to open the program or file directly. 您可以使用os.chdir()更改默认目录,并使用os.startfile()直接打开程序或文件。 As for saving it for later use, if you want to save it to a text file, I'll include it in my answer. 至于保存供以后使用,如果您想将其保存到文本文件中,我将在答复中包括它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM