I am trying this code:This code work fine and gave me the output what i need .
import os,sys
import sitenamevalidation
import getpass
import subprocess
site_name = sys.argv[1]
def get_location(site_name):
site_name_vali = sitenamevalidation.sitenamevalidation(site_name)
if site_name_vali == True:
pass
else:
print("Sitename should be in this format [a-z][a-z][a-z][0-9][0-9] example abc54 or xyz50")
sys.exit(0)
site = site_name[:3].upper()
user =getpass.getuser()
path = os.path.join('/home/',user,'location.attr')
if os.path.exists(path) == True:
pass
else:
print("Not able to find below path "+path)
#p1 = subprocess.Popen(['awk', '/site/{getline; print}' , path] ,stdout=subprocess.PIPE)
p1 = subprocess.Popen(['awk', '/ABC/{getline; print}' , path] ,stdout=subprocess.PIPE)
output=p1.communicate()
print str(output[0].strip().lower())
if __name__ == "__main__":
get_location(site_name)
Main issue is if i use this below command
p1 = subprocess.Popen(['awk', '/ABC/{getline; print}' , path] ,stdout=subprocess.PIPE)
where ABC is static value in path it work fine and give me desired output but i wanted the value which user will enter (site in my code). When i am doing this
p1 = subprocess.Popen(['awk', '/site/{getline; print}' , path] ,stdout=subprocess.PIPE)
It is looking for site in that file which is not there.I want the value of site variable to go in that command rather the site itself.Tried different by putting quote , double quote but nothing seem to be working.
Related
I'm trying to convert a python script into a standalone executable, and it stops at "error: invalid command 'bdist'"with both cx_freeze as well as with py installer.
I can not find what this error means, would you have any suggestions?
As requested in the comment, this is the main code, in Renaming_variables2 an external file is called, and if I don't add that file it gives an error. I think that implies exe runs at least untill that line. Then when I add the external file to it's requested location, that file-missing error is removed and the bdist error shows up. Without any line indication.
#Define variables
#Parameters:
import psutil
import os
import sys
import shutil
import time
print("the directory of the python installation = ", os.path)
print("the working directory of this project = ", os.getcwd())
print("the directory of this py = ", os.path.dirname(os.path.realpath(__file__)))
breaktime = 11
refresh_time = 2
nr_of_task_scheduler_commands = list()
for i in range(0,2):
nr_of_task_scheduler_commands.append(False)
name_check_md = "d.py"
convert_name_check_md = "Convert_d_py2exe.py"
path_name_check_md_and_conversion = "a/"
file_path = os.path.dirname(os.path.realpath(__file__))
file_path =file_path .replace('\\','/')
name_run_check = "ma.py"
convert_run_check = "Convert_s_py2exe.py"
path_run_check_and_conversion = "b/"
path_task = "task"
path_task_adm = "/adm"
name_original_task = "task.xml"
name_task = "task.xml"
temp_name_task = "taskq.xml"
adm_run_bat = "RunAsAdminConsole.bat"
output_adm= "/output_adm"
add_task_bat = "callrunasadminconsole4.bat"
import getpass
user_name = getpass.getuser()
import socket
domain = socket.getfqdn()
destination_path = "C:/out"
parent_output = 'output'
parent_dist = 'dist/'
#Not replaced below
name_check_md_exe = "ds.exe"
#Not replaced below
path_check_md_exe = "a/dist/"
name_run_check_exe = "dc.exe"
#Not replaced below
path_run_check_exe = "b/dist/"
#Not replaced below
path_min = "sx"
path_gmin = "dr"
md_output = "output.txt"
import getpass
print getpass.getuser()
#check if folder exists and if not, create it
if not os.path.exists(destination_path):
print destination_path + " is not an existing path"
# 1.0 if non-existant goto step 2
# 1.1 if existant: go to step 3.
# 2. read manual path from: "path.txt"
destination_path = raw_input("Please enter a new system path ")
# 3. feed path into Renaming_variables.py
# 4. run Renaming_variables.py
# Disable printing
def blockPrint():
sys.stdout = open(os.devnull, 'w')
import Renaming_variables2
# 5. run callerY.py
import caller5
os.chdir(file_path +'/'+ path_task+path_task_adm)
os.startfile("RunAsAdminConsole.bat")
#1.a #first delete destination file from /task if it exists
def deletion(file_path, old_file_name):
try:
# delete all files in folder
file_and_folder_path = os.path.join(file_path, old_file_name)
os.remove(file_and_folder_path)
except:
pass
deletion(file_path +'/'+ path_task,"RunAsAdminConsole.exe")
#1.b #then move file to /task
time.sleep(10)
shutil.move(file_path +'/'+ path_task+path_task_adm+output_adm+'/'+"RunAsAdminConsole.exe" , file_path +'/'+ path_task+'/')
os.chdir(file_path +'/'+ path_task+'/')
os.startfile("callrunasadminconsole4.bat")
However, I would like to stress that this question is not about solving that problem, this question is about the meaning of the following error message:
error: invalid command 'bdist'
I am currently trying to import my custom script into maya. I have found this example. My gole is to be able to edit script externally, reload it on each click.
I have tried the following script in python console, and it seems to work. Unfortunately it give me some error when I click the custom button in maya.
So this is the script of my custom button in maya
import sys
import os
def psource(module):
file = os.path.basename( module )
dir = os.path.dirname( module )
toks = file.split( '.' )
modname = toks[0]
# Check if dirrectory is really a directory
if( os.path.exists( dir ) ):
# Check if the file directory already exists in the sys.path array
paths = sys.path
pathfound = 0
for path in paths:
if(dir == path):
pathfound = 1
# If the dirrectory is not part of sys.path add it
if not pathfound:
sys.path.append( dir )
# exec works like MEL's eval but you need to add in globals()
# at the end to make sure the file is imported into the global
# namespace else it will only be in the scope of this function
exec ('import ' + modname) in globals()
# reload the file to make sure its up to date
exec( 'reload( ' + modname + ' )' ) in globals()
# This returns the namespace of the file imported
return modname
# When you import a file you must give it the full path
psource( '/The/correct/path/to/my/script/import_test_model.py' )
import_test_model.main()
while this is my custom script
def main():
print "funziona"
return
if __name__ == "__main__":
main()
this is error messages I'm getting
...
# When you import a file you must give it the full path
psource( '/Users/rostyslavkostyuk/Documents/developing/py_maya/import_test_model.py' )
import_test_model.main()
# Error: SyntaxError: file <string> line 1: invalid syntax #
# Error: invalid syntax #
# Error: invalid syntax #
# Error: invalid syntax #
I don't know what was the issue, but to solve it I just deleted the old button, and created the new one, and it started to work.
I just wrote a simple webscraping script to give me all the episode links on a particular site's page. The script was working fine, but, now it's broke. I didn't change anything.
Try this URL (For scraping ) :- http://www.crunchyroll.com/tabi-machi-late-show
Now, the script works mid-way and gives me an error stating, ' Element not found in the cache - perhaps the page has changed since it was looked up'
I looked it up on internet and people said about using the 'implicit wait' command at certain places. I did that, still no luck.
UPDATE : I tried this script in a demote desktop and it's working there without any problems.
Here's my script :-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from subprocess import Popen
#------------------------------------------------
try:
Link = raw_input("Please enter your Link : ")
if not Link:
raise ValueError('Please Enter A Link To The Anime Page. This Application Will now Exit in 5 Seconds.')
except ValueError as e:
print(e)
time.sleep(5)
exit()
print 'Analyzing the Page. Hold on a minute.'
driver = webdriver.Firefox()
driver.get(Link)
assert "Crunchyroll" in driver.title
driver.implicitly_wait(5) # <-- I tried removing this lines as well. No luck.
elem = driver.find_elements_by_xpath("//*[#href]")
driver.implicitly_wait(10) # <-- I tried removing this lines as well. No luck.
text_file = open("BatchLink.txt", "w")
print 'Fetching The Links, please wait.'
for elem in elem:
x = elem.get_attribute("href")
#print x
text_file.write(x+'\n')
print 'Links have been fetched. Just doing the final cleaning now.'
text_file.close()
CleanFile = open("queue.txt", "w")
with open('BatchLink.txt') as f:
mylist = f.read().splitlines()
#print mylist
with open('BatchLink.txt', 'r') as inF:
for line in inF:
if 'episode' in line:
CleanFile.write(line)
print 'Please Check the file named queue.txt'
CleanFile.close()
os.remove('BatchLink.txt')
driver.close()
Here's a screenshot of the error (might be of some help) :
http://i.imgur.com/SaANlsg.png
Ok i didn't work with python but know the problem
you have variable that you init -> elem = driver.find_elements_by_xpath("//*[#href]")
after that you doing some things with it in loop
before you finishing the loop try to init this variable again
elem = driver.find_elements_by_xpath("//*[#href]")
The thing is that the DOM is changes and you loosing the element collection.
I am following an example to read a config file from the following: https://wiki.python.org/moin/ConfigParserExamples
But I get a KeyError and can't figure out why. It is reading the files and I can even print the sections. I think I am doing something really stupid. Any help greatly appreciated.
Here is the code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
import logging
config_default=ConfigParser.ConfigParser()
class Setting(object):
def get_setting(self, section, my_setting):
default_setting = self.default_section_map(section)[my_setting]
return default_setting
def default_section_map(self,section):
dict_default = {}
config_default.read('setting.cfg')
sec=config_default.sections()
options_default = config_default.options(section)
logging.info('options_default: {0}'.format(options_default))
for option in options_default:
try:
dict_default[option] = config_default.get(section, option)
if dict_default[option] == -1:
print("skip: %s" % option)
except:
print("exception on %s!" % option)
dict_default[option] = None
return dict_default
return complete_path
if __name__ == '__main__':
conf=Setting()
host=conf.get_setting('mainstuff','name')
#host=conf.setting
print 'host setting is :' + host
My config file is named setting.cfg and looks like this:
[mainstuff]
name = test1
domain = test2
[othersection]
database_ismaster = no
database_master = test3
database_default = test4
[mysql]
port = 311111
user = someuser
passwd = somecrazylongpassword
[api]
port = 1111
And the Error is this...
exception on domain! Traceback (most recent call last): File
"./t.py", line 51, in
host=conf.get_setting('mainstuff','name') File "./t.py", line 14, in get_setting
default_setting = self.default_section_map(section)[my_setting] KeyError: 'name'
Be sure that your complete file path is setting.cfg. If you put your file in another folder or if it is named different, Python is going to report the same KeyError.
you have no general section. in order to get the hostname you need something like
[general]
hostname = 'hostname.net'
in your setting.cfg. now your config file matches the program -- maybe you prefer to adapt your porgram to match the config file? ...this should get you started at least.
UPDATE:
as my answer is useless now, here is something you could try to build on (assuming it works for you...)
import ConfigParser
class Setting(object):
def __init__(self, cfg_path):
self.cfg = ConfigParser.ConfigParser()
self.cfg.read(cfg_path)
def get_setting(self, section, my_setting):
try:
ret = self.cfg.get(section, my_setting)
except ConfigParser.NoOptionError:
ret = None
return ret
if __name__ == '__main__':
conf=Setting('setting.cfg')
host = conf.get_setting('mainstuff', 'name')
print 'host setting is :', host
This error occurs mainly due to 2 reasons:
Issue in reading the config file due to not getting the proper path. Absolute path may be used. Try reading the config file first whether any issue.
f = open("config.ini", "r")
print(f.read())
Not been able to find mentioned section in config file.
I'm using Python 2.7.2 and PyScriptor to create a script that I hope to use to first find a file of type (.shp) and then check to see if there is a matching file with the same name but with a (.prj) suffix. I'm stuck in the first part. I hope to share this script with others so I am trying to make the starting folder/directory a variable. Here's my script so far:
# Import the operating and system modules.
import os, sys
def Main():
# Retrieve the starting folder location.
InFolder = sys.argv[1]
#InFolder = "C:\\_LOCALdata\\junk"
os.chdir(InFolder)
print os.path.exists(Infolder)
print InFolder
# Begin reading the files and folders within the starting directory.
for root, dirs, files in os.walk(InFolder):
print os.path.exists(root)
for file in files:
print file
if file.endswith(".py"):
print os.path.join(root, file)
elif file.endswith(".xlsx"):
print os.path.join(root, file)
elif file.endswith(".shp"):
print "Shapefile present."
elif file.endswith(".txt"):
print "Text file present."
else:
print "No Python, Excel or Text files present."
return()
print "End of Script."
sys.exit(0)
if __name__ == '__main__':
sys.argv = [sys.argv[0], r"C:\_LOCALdata\junk"]
Main()
The only result I have gotten so far is:
*** Remote Interpreter Reinitialized ***
>>>
End of Script.
Exit code: 0
>>>
Any suggestions?
Layne
You're exiting your script before it even has a chance to get started!. Remove or indent these lines:
print "End of Script."
sys.exit(0)
Starting procedurally from the top (like python does), we:
Import libraries
Define the Main funcion.
Print a line, and immediately exit!
Your __name__ == '__main__' is never reached.
Oh man - duh. Thanks for the input. I did get it to work. The code searches for shapefiles (.shp) and then looks for the matching projection file (.prj) and prints the shapefiles that do not have a projection file. Here is the finished code:
# Import the operating and system modules.
import os, sys
def Main():
# Retrieve the starting folder location.
InFolder = sys.argv[1]
text = (InFolder + "\\" + "NonProjected.txt")
outFile = open(text, "w")
# Begin reading the files and folders within the starting directory.
for root, dirs, files in os.walk(InFolder):
for file in files:
if file.endswith(".shp"):
PRN = os.path.join(root,(str.rstrip(file,"shp") + "prj"))
if os.path.exists(PRN):
pass
else:
outFile.write((str(os.path.join(root,file))) + "\n")
print os.path.join(root,file)
else:
pass
outFile.close()
print
print "End of Script."
sys.exit(0)
return()
#================================================================================
if __name__ == '__main__':
sys.argv = [sys.argv[0], r"G:\GVI2Hydro"]
#sys.argv = [sys.argv[0], r"C:\_LOCALdata"]
#sys.argv = [sys.argv[0], r"U:"]
Main()