This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: open multiple files using “with open”?
Does, With open() not works with python 2.6
I have seen other answers to help me with this.
But I can't seem to get any of the solutions provided to work at all!
Here is part of my code for it:
with open('Levels.dat', 'w') as l, open('Names.dat', 'w') as n:
I got this error:
with open('Levels.dat', 'w') as l, open('Names.dat', 'w') as n:
^
SyntaxError: invalid syntax
How do I fix this?
Related
This question already has answers here:
How do I redirect stdout to a file when using subprocess.call in python?
(2 answers)
Closed 2 years ago.
Currently, I am using the following format to write the run results to a log file.
p = subprocess.run(["mpiexec -n 2 ./executor >log"],shell=True)
Could anyone tell me how to avoid using the "shell=True" while I can write a log file?
Thank you.
Just split the arguments yourself, open the file yourself, and pass the open file to run to make it send the output there:
with open('log', 'wb') as outfile:
p = subprocess.run(['mpiexec', '-n', '2', './executor'], stdout=outfile)
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 7 months ago.
I am new to Python and I am trying to use the 2 lines below.
book = xlrd.open_workbook('C:\files.xls')
sheet = book.sheet_by_name("Sheet2")
but I keep getting the 'c:\\files.xls' file not found. I tried the using (c:/file.xls),(r'C:\files.xls'), and os.path.normpath and still no joy.
Can anyone offer any assistance on how to deal with this? I know why it does python does it but not how to correct it in this context.
Any help would be greatly appreciated.
you can use pandas too acheive the samething
import pandas as pd(pip install pandas to install it)
book=pd.read_excel(r'C:/files.xls)
This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 5 years ago.
I'm somewhat new to Python, but I have enough under my belt to know what I'm doing. What I'm trying to do is write a few lines for a .txt file (as well as a variable), and then print 5 of those characters.
import os
username = "Chad_Wigglybutt"
file = open("testfile.txt", "w")
file.write("Hello .txt file, ")
file.write("This is a test, ")
file.write("Can this write variables? ")
file.write("Lets see: ")
file.write(username)
file.close()
It then creates the file without issue, but when I add
print file.read(5)
to the code, it gives me a syntax error for file.read, and I have no clue why. I've been on the internet for a few hours now and I can't find anything. Either I'm extremely bad at google searching and I'm an idiot, or something's broken, or both. Any tips/ideas? :/
You're writing Python 3 code. In Python 3, print is a function, not a special statement. You need parentheses for function calls:
print(file.read(5))
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 7 years ago.
I wanted to create a 10 files where each file has a "blob" word at the first sentence and read those sentence directly.
Here's my code:
import random
import string
for i in range(9):
name = input('fileNumber')+ str(i+1) + '.txt'
try:
file = open(name,'w+')
file = open(name,'a')
file.write("blob")
file = open(name,'r')
print file.read() #'file' being highlighted with red color when I execute
file.close()
When I run it, I got an error message saying Invalid syntax and it highlights my file.read() line.
Can somebody tell me where's the flaw in my code?
EDIT: I'm currently using python 3.5. However, I could also switch to 2.7 as well!
Try doing this:
print(file.read())
In Python 3.x print() is a function and the parentheses are mandatory.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Syntax error on print with Python 3
I want to view the contents of a .tgz file and I found python's tarfile module. I found the following tutorial which looked promising. http://www.doughellmann.com/PyMOTW/tarfile/
Here is my python file below:
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print tar.getnames()
When I actually execute my python file, I get a carrot sign pointing at the 'r' in the last line and the error message: SyntaxError: invalid syntax.
Print is function in python 3.x.
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print(tar.getnames())