This question already has answers here:
Writing string to a file on a new line every time
(19 answers)
Closed 3 months ago.
I'm trying to use the f.write keyword, I want each thing I write to be in a new line so I did this:
f.write('',message_variable_from_previous_input,'\n')
However, after I ran this it threw back an error saying the following:
Traceback (most recent call last):
File "c:\Users\User1\OneDrive\Desktop\coding\folder_namr\file_name.py", line 5, in <module>
f.write('',msg,'\n')
TypeError: TextIOWrapper.write() takes exactly one argument (3 given)
Does anybody know haw to fix this?
write method takes exactly one argument
so you should write like this:
f.write(f"{message_variable_from_previous_input}\n")
or:
f.write(str(message_variable_from_previous_input) + "\n")
Related
This question already has an answer here:
How to get bash arguments with leading pound-sign (octothorpe)
(1 answer)
Closed 1 year ago.
I am trying to pass a command line argument like this because the actual string contains a # symbol before and after it
python3 test.py #fdfdf#
This is the error message I am getting
Traceback (most recent call last):
File "test.py", line 3, in <module>
print(sys.argv[1])
IndexError: list index out of range
How can I pass a string which contains a # symbol in the beginning and the end as a command line argument?
Update:
OS: Ubuntu 20.04
You can try one of these and see which one works:
python3 test.py "#fdfdf#" or python3 test.py \#fdfdf\#
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 3 years ago.
I'm trying to remove a directory and I guess i'm having an issue with defining the path for the shutil.rmtree(). Does this function take the relative path or the absolute. I use win10 and am having problem understanding how the paths can and should be defined in python, because it seems that for every function the path needs to be defined in a different way.
import shutil
dir_to_remove = "C:\\Users\name_of_user\\dir_to remove"
shutil.rmtree(dir_to_remove)
Result is:
Traceback (most recent call last):
File "<ipython-input-257-9cc5f9fe51d2>", line 1, in <module>
shutil.rmtree(dir_to_remove)
TypeError: 'str' object is not callable
Any help or guidance is appreciated.
You are missing the second backslash after C:\\Users.
dir_to_remove = "C:\\Users\\name_of_user\\dir_to remove"
shutil.rmtree(dir_to_remove)
This question already has answers here:
Attribute Error: next()
(2 answers)
Is generator.next() visible in Python 3?
(3 answers)
Closed 4 years ago.
This is my Python code. If the line has : I'm want to skip this line and the next line. I'm trying to use file.next since file is an iterator. Why is it not working?
file=open("example.txt","r") //output of ls -R
currentDirectory=""
for line in file:
if ":" in line:
currentDirectory=line[:-1]
file.next() # Error in this line
continue
A=line.split()
print(A)
print(len(A))
print('insert into files set name=${name}, inode=${inode}, isDir=${isDir},size=${size}'
.format(inode=A[0],name=A[9],isDir="d" in A[1],size=A[5]))
The error:
Traceback (most recent call last):
File "ls-to-sql.py", line 7, in <module>
file.next()
AttributeError: '_io.TextIOWrapper' object has no attribute 'next
I'm sure there are many other ways to implement the same logic. The question is: why this is not working. And if there is another iteration way to solve it.
This question already has answers here:
Why does Python allow mentioning a method without calling it?
(6 answers)
Closed 4 years ago.
My file is not being closed properly and I cannot figure out why:
open_sample_file = codecs.open(ssis_txt_files_2[a], 'r', "utf-16")
whatever = open_sample_file.readlines()
open_sample_file.close
print(open_sample_file)
output:
<codecs.StreamReaderWriter object at 0x0331F3B0>
Shouldn't the output return None?
You have to call
open_sample_file.close()
For one, you need to call the close method:
open_sample_file.close()
Then, what will result is a closed file, not None.
>>> open_sample_file
<closed file 'filename', mode 'r' at 0x109a965d0>
Finally, the usual way to handle files in Python is using with-statements, which take care of closing the file for you:
with codecs.open(filename) as open_sample_file:
# do work
# the file will be closed automatically when the block is done
This question already has answers here:
Python MySQLdb TypeError: not all arguments converted during string formatting
(8 answers)
Closed 6 years ago.
We re-installed one of our servers and a newer version of python was installed. Since that happened, we get this error:
Traceback (most recent call last):ogress: 0%
File "/var/www/scripts/apollo_file_management_v3_FC.py", line 368, in <module>
main()
File "/var/www/scripts/apollo_file_management_v3_FC.py", line 362, in main
delivered()
File "/var/www/scripts/apollo_file_management_v3_FC.py", line 223, in delivered
if cursor_2.execute("SELECT * FROM `table1` WHERE `wt` = ?",(d[0])):
File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 187, in execute
TypeError: not all arguments converted during string formatting
The guy that wrote the script doesn't work here anymore and the rest of us have never programmed in python.
Please let me know if you require additional info in order to assist.
PEP 249: Python Database API Specification v2.0
The second argument to execute() must be a sequence.
(d[0],)