Trying to make a log function - python

I want to make a log function in my script but I don`t know why it gives me this error
File "c:/Users/x/x/x", line 33
log = open(f"Log/{realDate.strftime("%x")}.txt", "a")
^
SyntaxError: invalid syntax
Here`s the code that is causing the problem
realDate = datetime.datetime.now()
log = open(f"Log/{realDate.strftime("%x")}.txt", "a")
log.write("Hello Stackoverflow!")
log.close()
Can somebody please help me?

The problem is that you are trying to nest double quotes inside double quotes; f-strings don't allow that. You would need to change one or the other to another style of quotes. For example:
log = open(f'Log/{realDate.strftime("%x")}.txt', "a")
The main problem is that you aren't using a version of Python that supports f-strings. Make sure you are using Python 3.6 or later. (Update: Even in Python 3.6 or later, the identified location of the error can shift:
>>> f"{print("%x")}"
File "<stdin>", line 1
f"{print("%x")}"
^
SyntaxError: invalid syntax
>>> f"{print("x")}"
File "<stdin>", line 1
f"{print("x")}"
^
SyntaxError: invalid syntax
)

Related

invalid syntax (<string>, line 4) (syntax-error)

I have an invalid syntax according to flake8, pylint, but the code works.
What's wrong with that code?
I did lots of Google search but couldn't find anything.
#!/usr/bin/env python
with open("test.py", "a") as output:
# E: 4, 0: invalid syntax (<string>, line 4) (syntax-error)
print("hello world", file=output)
What version of Python are you running? I'm not sure when it was implemented exactly but I don't think earlier versions of Python had a file=output parameter for the print() function so your interpreter might only be expecting a string

Python Def Syntax Error with . in file name

In my Visual Code Studio running python3.6 - my code is saved as "Langemead12Test.py" w/ lines as:
!C:\Users\Bones\Anaconda3\python.exe
[1]def readFastq(SRR835775_1.first1000.fastq)
Red Error underline def [pylint] E0001:invalid syntax (, line 3).
In Anaconda command prompt running python3.6:
File "Langmead12Test.py", line 3
def readFastq(SRR835775_1.first1000.fastq)
^
SyntaxError: invalid syntax
Question(s): I'm a total newb here but I can't seem to understand why VCS throws an error under def and my anaconda command line prompt throws error at the . within the fastq filename. Are these independent errors or different representations of the same error? All thoughts and hints are welcome.
Background: I am attempting to execute exercise in ADS1: Practical: Working with sequencing reads. Ben Langmead's Youtube class on reading fastq files (filename = SRR835775_1.first1000.fastq).
You have 2 syntax error in one line:
1.Missing "" or this '' before and after your string
2.You forget to put ":" and the end of the line:
Correct syntax:
def readFastq(filename="SRR835775_1.first1000.fastq"):
Read this carefully please:
https://docs.python.org/3/tutorial/introduction.html#strings
https://docs.python.org/3/tutorial/controlflow.html#defining-functions
And after that read and learn everything from there:
https://docs.python.org/3/tutorial/index.html

Getting a SyntaxError when trying out "with" in Python3.6

When attempting to open an existing text file (myfile.txt) with "with":
with open ('myfile.txt') as my_new_file:
I get the following error:
File "<ipython-input-263-06288b4ea914>", line 1
with open ('myfile.txt') as my_new_file:
^
SyntaxError: unexpected EOF while parsing
However, I am able to open the file using "open":
myfile = open('myfile.txt')
myfile.read()
'Hello, This is a text file\nThis is the second line\nThis is the third line'
Can someone point out what it is that I am doing wrong? Thanks!
Alls you need to do is do something after the colon. Finish the thought.
$ python -c "with open ('myfile.txt') as my_new_file:"
File "<string>", line 1
with open ('myfile.txt') as my_new_file:
^
SyntaxError: unexpected EOF while parsing
$ python -c "with open ('myfile.txt') as my_new_file: print(my_new_file.readlines())"
['hey\n', 'you\n']
An EOF error is an unexpected End Of File error where, the Python interpreter was expecting something part of the with statement on the next line.
For example:
with open ('myfile.txt') as my_new_file:
my_new_file.read()
is valid. Since the with statement has something in it, whereas:
with open ('myfile.txt') as my_new_file:
my_new_file.read()
Will return an error since there is nothing part of the with, when there should be at least one function in order to use the flow control.
If you have nothing to do then you should call pass. This has nothing to do with the file you want to read. Exactly the same effect can be made by using any statement that requires an indent, without the indent:
for i in range(100):
print('Hello')

Call python script from command line, which exists in another directory

My project structure:
/Users/user1/home/bashScrpts/shellScript.sh
/Users/user1/home/pyScrpts/pyScrpt.py
From the shell script I want to call a function of pyScrpt.py
Content of
pyScrpt.py
def test():
return sys.argv[1]
shellScript.sh
DATA="testXX"
cmd="import sys;sys.path.insert(0, '/Users/user1/home/pyScrpts/pyScrpt');import pyScrpt; print pyScrpt.test()"
xy=$(python -c \'${cmd}\' "${DATA}")
echo $xy
Error I am getting:
File "<string>", line 1
'import
SyntaxError: EOL while scanning string literal
I don't see whats going wrong here.
Can anyone help me on this??
You just need to replace \' in \'${cmd}\' with double quotes "${cmd}".
Also you should add import sys to your pyScrpt.py.
I have never done this before, but i would hazard a guess that it may be due to the wrong function structure, it should read:
def test()
return sys.argv[1]

Python Syntax error while execution

I am getting a syntax error for the line below i am not sure why this error is getting generated as earlier it used to work fine and suddenly it started throwing this error
myfile.write('\n'+str(serverAndInstance)+','+str(GCOverHead)+','+str(min(usedAfterGc))+','+str(max(usedAfterGc))+','+str(sum(usedAfterGc)/len(usedAfterGc))+',0,0')
Your line, as posted, works just fine, which means that the preceding line is missing a closing ), ] or } parenthesis, bracket or brace:
>>> something = (something_else + str(whatever)
... myfile.write('\n'+str(serverAndInstance)+','+str(GCOverHead)+','+str(min(usedAfterGc))+','+str(max(usedAfterGc))+','+str(sum(usedAfterGc)/len(usedAfterGc))+',0,0')
File "<stdin>", line 2
myfile.write('\n'+str(serverAndInstance)+','+str(GCOverHead)+','+str(min(usedAfterGc))+','+str(max(usedAfterGc))+','+str(sum(usedAfterGc)/len(usedAfterGc))+',0,0')
^
SyntaxError: invalid syntax
Here it is the something line that is missing a closing parenthesis (2 times ( but only one )) but Python won't know it is missing until parsing the next line.
You really should use the csv module instead to write comma-separated values to a file, your line would be a lot more readable if you used:
with open(outputfile, 'wb') as outfh:
csvwriter = csv.writer(outfh)
for something in some_iterable:
# ...
row = [serverAndInstance, GCOverHead, min(usedAfterGc), max(usedAfterGc),
sum(usedAfterGc) / len(usedAfterGc), 0, 0]
csvwriter.writerow(row)

Categories