I am having an issue with the following section of Python code:
var socketio = require('socket.io);
var io = socketio.listen(server);
var EE = require('events').EventEmitter;
var ee = new EE(); //this is actually initialized elsewhere but now you know what it is
var connectedUsers = {}; //associative array to store socket connections by socket.id
io.on('connection', function (socket) {
connectedUsers[socket.id] = socket; //should I bother storing the socket connections in the associative array or just leave it to the closure to store them
socket.on('disconnect', function () {
connectedUsers[socket.id] = null; //should I bother removing
});
ee.on('update',function(data){
socket.emit('update',JSON.stringify(data));
});
ee.on('insert',function(data){
socket.emit('insert',JSON.stringify(data));
});
ee.on('delete',function(data){
socket.emit('delete',JSON.stringify(data));
});
});
}
Specifically the error is as follows:
# Open/Create the output file
with open(sys.argv[1] + '/Concatenated.csv', 'w+') as outfile:
try:
with open(sys.argv[1] + '/MatrixHeader.csv') as headerfile:
for line in headerfile:
outfile.write(line + '\n')
except:
print 'No Header File'
I've done some research and it seems that the Traceback (most recent call last): File "ConcatenateFiles.py", line 12, in <module> with open(sys.argv[1] + 'Concatenated.csv', 'w+') as outfile:
IndexError: list index out of range
might require an argument at the command line when running the script, but I'm not sure what to add or what the issue might be! I've also searched the site, but all of the solutions I've found either have no comments and/or don't include the open function as mine does.
Any help is greatly appreciated.
答案 0 :(得分:8)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="0"
android:id="@+id/btn_delete"
android:drawableStart="@drawable/ic_delete"
android:drawableLeft="@drawable/ic_delete"
style="?android:attr/borderlessButtonStyle"/>
represents the command line options you execute a script with.
sys.argv
is the name of the script you are running. All additional options are contained in sys.argv[0]
.
You are attempting to open a file that uses sys.argv[1:]
(the first argument) as what looks to be the directory.
Try running something like this:
sys.argv[1]
答案 1 :(得分:5)
vm.updateMaxOptions = function() {
vm.maxOptions = [];
for (var i = player.max_points - 2; i < customer.max_points + 3; i++) {
vm.maxOptions.push(i);
}
};
vm.changeMax = function() {
playersService.setMaxPoints({
playerId: playerId,
max: vm.selectedMax
}, {}).$promise.then(function(res){
vm.player.max_points = vm.selectedMax;
vm.updateMaxOptions();
return res.success;
}, function(res) {
alert('Couldn\'t update number of points to ' + vm.selectedMax + ':' + res.success);
});
};
is the list of command line arguments passed to a Python script, where sys.argv
is the script name itself.
It is erroring out because you are not passing any commandline argument, and thus sys.argv[0]
has length 1 and so sys.argv
is out of bounds.
To "fix", just make sure to pass a commandline argument when you run the script, e.g.
sys.argv[1]
However, you likely wanted to use some default directory so it will still work when you don't pass in a directory:
python ConcatenateFiles.py /the/path/to/the/directory
答案 2 :(得分:3)
I've done some research and it seems that the sys.argv might require an argument at the command line when running the script
Not might, but definitely requires. That's the whole point of #Written by : Pamal Mangat.
#Written on : Monday, July 27th, 2015.
#Rock Paper Scissors : Version 1.2 (Tkinter [GUI] addition)
from tkinter import *
from sys import *
from PIL import Image, ImageTk
import pygame as py
import os
from random import randrange
py.init()
#Function runs the actual game.
def runGame(startWindow):
#Close [startWindow] before advancing:
startWindow.destroy()
startWindow.quit()
master = Tk()
master.title('Lets Play!')
#Function carries on the remainder of the game.
def carryGame(button_id):
result = StringVar()
printResult = Label(master, textvariable = result, font='Bizon 32 bold', bg='PeachPuff2')
printResult.place(x=150, y=300)
#Computer's move:
random_Num = randrange(1,4)
if random_Num == 1:
computer_Move = 'Rock'
elif random_Num == 2:
computer_Move = 'Paper'
else:
computer_Move = 'Scissors'
if button_id == 1:
player_Move = 'Rock'
elif button_id == 2:
player_Move = 'Paper'
else:
player_Move = 'Scissors'
#Rock button
rock_Button = Button(master, width=15, height=7, command=lambda:carryGame(1))
rock_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\rock.png')
rock_Button.config(image=rock_photo,width="120",height="120")
rock_Button.place(x=17, y=70)
#Paper button
paper_Button = Button(master, width=15, height=7, command=lambda:carryGame(2))
paper_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\paper.png')
paper_Button.config(image=paper_photo,width="120",height="120")
paper_Button.place(x=167, y=70)
#Scissors button
scissors_Button = Button(master, width=15, height=7, command=lambda:carryGame(3))
scissors_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\scissors.png')
scissors_Button.config(image=scissors_photo,width="120",height="120")
scissors_Button.place(x=317, y=70)
label_1 = Label(master, text='Please make your selection-', font='Bizon 20 bold', bg='PeachPuff2')
label_1.pack(side=TOP)
label_2 = Label(master, text='The computer picked:', font='Helvetica 22 bold', bg='PeachPuff2')
label_2.place(x=70, y=240)
#Locks window size
master.maxsize(450, 400)
master.minsize(450, 400)
#Sets window background to PeachPuff2
master.config(background='PeachPuff2')
master.mainloop()
def startScreen():
#Plays music for the application
def playMusic(fileName):
py.mixer.music.load(fileName)
py.mixer.music.play()
#Start Window
startWindow = Tk()
startWindow.title('[Rock] [Paper] [Scissors]')
#Imports image as title
load = Image.open(r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\title.png')
render = ImageTk.PhotoImage(load)
img = Label(startWindow, image=render, bd=0)
img.image = render
img.place(x=-100, y=-65)
clickToPlay = Button(startWindow, text='Play!', width=8, font='Bizon 20 bold', bg='Black', fg='Yellow', relief=RIDGE, bd=0, command=lambda:runGame(startWindow))
clickToPlay.place(x=75, y=125)
#Credit
authorName = Label(startWindow, text='Written by : Pamal Mangat', font='Times 6 bold', bg='Black', fg='Yellow')
authorName.place(x=2, y=230)
versionNum = Label(startWindow, text='[V 1.2]', font='Times 6 bold', bg='Black', fg='Red')
versionNum.place(x=268, y=230)
#Start Screen Music
playMusic(r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Audio\title_Song.mp3')
#Locks window size
startWindow.maxsize(300, 250)
startWindow.minsize(300, 250)
#Sets window background to black
startWindow.config(background='Black')
startWindow.mainloop()
startScreen()
, it contains the command line arguments. Like any python array, accesing non-existent element raises sys.argv
.
Although the code uses IndexError
to trap some errors, the offending statement occurs in the first line.
So the script needs a directory name, and you can test if there is one by looking at try/except
and comparing to 1+number_of_requirements. The argv always contains the script name plus any user supplied parameters, usually space delimited but the user can override the space-split through quoting. If the user does not supply the argument, your choices are supplying a default, prompting the user, or printing an exit error message.
To print an error and exit when the argument is missing, add this line before the first use of sys.argv:
len(sys.argv)
if len(sys.argv)<2:
print "Fatal: You forgot to include the directory name on the command line."
print "Usage: python %s <directoryname>" % sys.argv[0]
sys.exit(1)
always contains the script name, and user inputs are placed in subsequent slots 1, 2, ...
see also: