I am a bit new to coding, I am trying to improve my skills. My preferred programming language is python. But I do need help with this bit of code. I am making an app called User Helper for my own use. 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.
I have tried many different methods including using the os function. 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. As for saving it for later use, if you want to save it to a text file, I'll include it in my answer.
Related
I'm making a python script that will automatically post to Instagram. It's a skateboarding niche page and I want to give credit to people I repost... (I'm posting thrue facebook creator studio)
This is how the video picker looks like...
skatenr = int(skatenr) + int("1")
SendKeys = "C://Users/User/PycharmProjects/skateboard auto post/skate/"+str(memenr)+".mp4"
driver.find_element_by_xpath('/html/body/div[6]/div/div/div/div/div/a/div[2]/input').send_keys(SendKeys)
You could see how I got it to take the next post from the folder I store my vids in. My idea was that I store the captions in another folder and name it skate1.txt skate2.txt ...... then like the videos it takes the notepad and puts it in the caption place. How do I make Python pick the next notepad.
def read_line():
f = open('skate1.txt')
The = (f.read())
return The
print(read_line())
any ideas?
Regards your friendly guy who started coding yesterday
if you run the code at all time you can use this formular
from os import path
x=0
The=[]
while path.exists('skate%s.txt'%str(x)):
with open("skate%s.txt"%str(x),'r') as file:
k=file.read()
The.append(k)
x+=1
now all of your caption will be in the "The" list
I have no training in python. I know QBasic (yes I am 73!) but I have a hard time grasping OOP, and classes. I still think with QBasic ideas.
I want to make a simple prog to enter alphanumeric data using keyboard mainly, then process it into a txt file. I want to use it as stand alone outside python. I can understand a bit of canvas, keyboard etc. but cannot put them together to make a "box on the screen that tells what to do and sends stuff into the processing algorithm". I just can't see the big picture. Does anyone have some simple examples that I might understand and use? The processing of data I can do.
Jon
#when this is ran it will ask What would you like to input and whatever you type will be set as the variable input
input = input("What would you like to input")
#this creates a txt file
file = open("textfilename.txt", "w")
#this will get the variable input and write it in the text file
file.write('input')
#this closes the txt file
file.close()
I have written piece of code which runs sextractor from python, however I only know how to do this for one file, and i need to loop it over 62 files. Im not sure how i would go about doing this. I have attached my code bellow:
#!/usr/bin/env python
# build a catalog using sextractor on des image here
sys.path.append('/home/fitsfiles') #not sure if this does anything/is correct
def sex(image, output, sexdir='/home/sextractor-2.5.0', check_img=None,config=None, l=None) :
'''Construct a sextractor command and run it.'''
#creates a sextractor line e.g sex img.fits -catalog_name -checkimage_name
q="/home/fitsfiles/"+ "01" +".fits"
com = [ "sex ", q, " -CATALOG_NAME " + output]
s0=''
com = s0.join(com)
res = os.system(com)
return res
img_name=sys.argv[0]
output=img_name[0:1]+'_star_catalog.fits'
t=sex(img_name,output)
print '----done !---'
so this code produces a command in my main terminal of, sex /home/fitsfiles/01.fits -CATALOG_NAME g_star_catalog.fits
which successfully produces a star catalogue as I want.
However I want my code to to this for 62 fits files and change the name of star_catalog.fits depending upon which fitsfile is being used. any help would be appreciated.
There are many ways you could approach this. Let's assume you want to run your script as something like
python extract_stars.py /home/fitsfiles/*.fits
Then, you could try something like this:
for arg in len(sys.argv):
filename = arg.split('/')[-1].strip('.fits')
t = sex(arg, filename +'_star_catalog.fits')
# Whatever else
This assumes that you remove the line in sex that reformats the input filename. Also, you do not need to append the fits directory to your path.
The alternative approach is, if you do not plan to do anything else in python, you could write a bash script which would really simplify the task.
And, as a side note, you if you had asked this question more generally (ie, I wish to apply a function I wrote to a number of input files) and without reference to a rather uncommonly used application, you would have likely received an answer much more quickly.
The community has now developed some python wrappers which allow you to run sextractor as if it was a python command. These are: pysex, sewpy and astromatic_wrapper.
The good thing about sextractor wrappers is that allow you to write much cleaner code without the need of defining extra functions, invoking os commands or having the configuration files and the outputfiles on your machine. Moreover, the output can be an astropy table, a pandas dataframe or a numpy array.
For your specific case, you could use pysex and do:
import pysex
import glob
filelist = glob.glob('/directory/*.fits')
for fitsfile in filelist:
cat = pysex.run(fitsfile, params=['X_IMAGE', 'Y_IMAGE', 'FLUX_APER'],
conf_args={'PHOT_APERTURES':5})
print cat['FLUX_APER']
Is there a simple module that let's you paste input in python ?
Asking someone to type letter by letter is kinda harsh .
By default a .py file is opened with python.exe if is installed and this does not allow "rightclick+paste" in the console .So how can i make this happen with python ? i think this would be more of a exact question .
You can make this by open cmd.exe and type here "C:\Python32\python".
Path is depend on the version of python. Mine is 3.2.
If you're looking for a way to simply paste something into the windows command prompt, John Giotta is correct that a user can click on the little icon in the top left.
I imagine, however, that you're looking for a way that a user can input a large amount of text, without typing it in line by line. A simple way to do this, would be to let the user input a file name, which python would then read. Perhaps something like this is what you're looking for:
while True:
filename = raw_input("Path to file to be read: ")
try:
with open(filename, 'rb') as f:
contents = f.read()
break
except IOError:
print "That was not a valid file \n"
This loop will keep asking the user for a file until then enter a valid path. When they enter a valid path, it will be read in as a string to the contents variable. This way, a user could enter a large amount of text into a file, and then you simply prompt them for the path to the file.
You can read up on file input more In the docs.
This is my first time using this so be kind :) basically my question is I am making a program that opens many Microsoft Word 2007 docs and reads from a certain table in that document and writes that info to an excel file there is well in excess of 1000 word docs. I have all of this working but the only problem when I run my code it does not close MSword after opening each doc I have to manually do this at the end of the program run by opening word and selecting exit word option from the Home menu. Another problem is also if a run this program consecutively on the second run everything goes to hell it prints the same thing repeatedly no matter which doc is selected I think this may have to do with how MSword is deciding which doc is active e.g. is it still opening the last active document that was not closed from the last run. Anyways here is my code for the opening and closing part I wont bore you guys with the rest::
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = 0
# Open a specific file
#myWordDoc = tkFileDialog.askopenfilename()
MSWord.Documents.Open("C:\\Documents and Settings\\fdosier" + chosen_doc)
#Get the textual content
docText = MSWord.Documents[0].Content
charText = MSWord.Documents[0].Characters
# Get a list of tables
ListTables = MSWord.Documents[0].Tables
------Main Code---------
MSWord.Documents.Close
MSWord.Documents.Quit
del MSWord
Basically, Python is not VBA, so this:
MSWord.Documents.Close
is equivalent to:
getattr(MSWord.Documents, "Close")
i.e. you just get some method object and do nothing with it. You need to call the method with the call operator (the parentheses :) :
MSWord.Documents.Close()
Accordingly for .Quit.
Before your MSWord.Quit did you try using:
MSWord.ActiveWindow.Close
Or even more simpley just doing
MSWord.Quit
I dont really understand if you are trying to close a document or the application.
I think you need a MSWord.Quit at the end (before and/or instead of the the del)