Syntax error for "with open(file, 'r+'):" - python

I have a bunch of .csh in a directory and I want to open them one by one, search for "//" and replace it with "/" with a python script. How do I do that?
I tried:
import os
for file in os.listdir("./"):
if file.endswith(".csh"):
with open(file, 'r+'):
data = file.read().replace("//", "/")
f.write(data)
f.close()
But it gives me:
File "script.py", line 4
with open(file, 'r+'):
^
SyntaxError: invalid syntax

You are using an old version of Python. The with statement was introduced in Python 2.5, where it had to be enabled via
from __future__ import with_statement
It is best to upgrade to 2.7, if you need to stay in the 2.x line, or 3.4.
Note that you also need to change your code according the answer by Avinash Raj, capturing the file object in a variable via as f. file.read() will not work because file continues to be the file name string.

Change your code to,
import os
for file in os.listdir("./"):
if file.endswith(".csh"):
with open(file, 'r+') as f:
data = f.read()
f.seek(0)
with open(file, 'w+') as w:
dat = data.replace("//", "/")
w.write(dat)

Related

Issue reading file in Python

I had some problems with my Python code.
My code:
import os
logininfo = list()
with open(os.getcwd() + '/login/info.txt', 'r') as f:
logininfo = f.readlines()
But my code isn’t work, so how do I fix that?
Edit: I changed the quote and changed the ‘ to '
Problem 2: After I fix all that look like my computer is freeze now and I can’t even move my mouse. After freeze for a while, the code make my computer ran to BSOD. What happened?
Okay, I think I see what the problem in problem 2 that my file was too big with 50 GB of login information of my server. Thanks you guy for helping me solve the problem 1.
Your problem is likely that your / (forward slashes) are supposed to be \ (backslashes). It is also a good practice to use os.path.join() when concatenating file paths. Make sure login\info.txt does not have a backslash in front of it. I printed the list afterwards to make sure it was working. Windows file paths use \\.
import os
with open(os.path.join(os.getcwd(), 'login\info.txt'), 'r') as f:
logininfo = f.readlines()
print(logininfo)
Regardless of OS, I would recommend using pathlib module to deal with system paths and to decrease ambiguity in OS path handling.
So, regardless of OS, an API would be (including your code):
from pathlib import Path
file_path = Path.cwd() / 'login' / 'info.txt'
with open(file_path, 'r') as f:
login_info = f.readlines()
Get familiar with that module (it's out of the box!) here: https://docs.python.org/3/library/pathlib.html
I believe the wrong thing is that you're using double slashs, when it should be:
import os
with open(os.getcwd() + ‘/login/info.txt’, 'r') as f:
logininfo = f.readlines()
I reproduced the error here, created a file with the same folder structure as yours, and this definitely should work:
In [3]: with open(os.getcwd() + '/login/info.txt', 'r') as f:
...: lines = f.readlines()
...: for line in lines:
...: print(line)
...:
Olá,
deixa eu ver esse erro aqui

Using fileinput to write to a file makes issue

for line in fileinput.FileInput("file.txt", inplace=1):
if "success" in line:
print(line)
When I use fileinput, the file 'file.txt' is not released. I could see the issue 'file.txt' still in use.
When I do the above function using normal file operation , no issue is shown
How to fix the issue with fileinput.
I used the below code snippet , but the issue is showing again . The file is not getting closed I guess
f = fileinput.input("file.txt", inplace=1)
for line in f:
if "success" in line:
print(line)
f.close()
In Python 2.7, you have to explicitly call close() on the fileinput instance:
try:
f = fileinput.input("file.txt", inplace=1)
for line in f:
if "success" in line:
print line, end=""
else:
f.close()
Since Python 3.2, the FileInput class can be used as a context manager. See the fileinput documentation for more information.
with fileinput.input(files=('file.txt'), inplace=1) as f:
for line in f:
if "success" in line:
print(line)

Error when trying to read and write multiple files

I modified the code based on the comments from experts in this thread. Now the script reads and writes all the individual files. The script reiterates, highlight and write the output. The current issue is, after highlighting the last instance of the search item, the script removes all the remaining contents after the last search instance in the output of each file.
Here is the modified code:
import os
import sys
import re
source = raw_input("Enter the source files path:")
listfiles = os.listdir(source)
for f in listfiles:
filepath = source+'\\'+f
infile = open(filepath, 'r+')
source_content = infile.read()
color = ('red')
regex = re.compile(r"(\b be \b)|(\b by \b)|(\b user \b)|(\bmay\b)|(\bmight\b)|(\bwill\b)|(\b's\b)|(\bdon't\b)|(\bdoesn't\b)|(\bwon't\b)|(\bsupport\b)|(\bcan't\b)|(\bkill\b)|(\betc\b)|(\b NA \b)|(\bfollow\b)|(\bhang\b)|(\bbelow\b)", re.I)
i = 0; output = ""
for m in regex.finditer(source_content):
output += "".join([source_content[i:m.start()],
"<strong><span style='color:%s'>" % color[0:],
source_content[m.start():m.end()],
"</span></strong>"])
i = m.end()
outfile = open(filepath, 'w+')
outfile.seek(0)
outfile.write(output)
print "\nProcess Completed!\n"
infile.close()
outfile.close()
raw_input()
The error message tells you what the error is:
No such file or directory: 'sample1.html'
Make sure the file exists. Or do a try statement to give it a default behavior.
The reason why you get that error is because the python script doesn't have any knowledge about where the files are located that you want to open.
You have to provide the file path to open it as I have done below. I have simply concatenated the source file path+'\\'+filename and saved the result in a variable named as filepath. Now simply use this variable to open a file in open().
import os
import sys
source = raw_input("Enter the source files path:")
listfiles = os.listdir(source)
for f in listfiles:
filepath = source+'\\'+f # This is the file path
infile = open(filepath, 'r')
Also there are couple of other problems with your code, if you want to open the file for both reading and writing then you have to use r+ mode. More over in case of Windows if you open a file using r+ mode then you may have to use file.seek() before file.write() to avoid an other issue. You can read the reason for using the file.seek() here.

Confusing Error when Reading from a File in Python

I'm having a problem opening the names.txt file. I have checked that I am in the correct directory. Below is my code:
import os
print(os.getcwd())
def alpha_sort():
infile = open('names', 'r')
string = infile.read()
string = string.replace('"','')
name_list = string.split(',')
name_list.sort()
infile.close()
return 0
alpha_sort()
And the error I got:
FileNotFoundError: [Errno 2] No such file or directory: 'names'
Any ideas on what I'm doing wrong?
You mention in your question body that the file is "names.txt", however your code shows you trying to open a file called "names" (without the ".txt" extension). (Extensions are part of filenames.)
Try this instead:
infile = open('names.txt', 'r')
As a side note, make sure that when you open files you use universal mode, as windows and mac/unix have different representations of carriage returns (/r/n vs /n etc.). Universal mode gets python to handle this, so it's generally a good idea to use it whenever you need to read a file. (EDIT - should read: a text file, thanks cameron)
So the code would just look like this
infile = open( 'names.txt', 'rU' ) #capital U indicated to open the file in universal mode
This doesn't solve that issue, but you might consider using with when opening files:
with open('names', 'r') as infile:
string = infile.read()
string = string.replace('"','')
name_list = string.split(',')
name_list.sort()
return 0
This closes the file for you and handles exceptions as well.

Replace and overwrite instead of appending

I have the following code:
import re
#open the xml file for reading:
file = open('path/test.xml','r+')
#convert to string:
data = file.read()
file.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>",data))
file.close()
where I'd like to replace the old content that's in the file with the new content. However, when I execute my code, the file "test.xml" is appended, i.e. I have the old content follwed by the new "replaced" content. What can I do in order to delete the old stuff and only keep the new?
You need seek to the beginning of the file before writing and then use file.truncate() if you want to do inplace replace:
import re
myfile = "path/test.xml"
with open(myfile, "r+") as f:
data = f.read()
f.seek(0)
f.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>", r"<xyz>ABC</xyz>\1<xyz>\2</xyz>", data))
f.truncate()
The other way is to read the file then open it again with open(myfile, 'w'):
with open(myfile, "r") as f:
data = f.read()
with open(myfile, "w") as f:
f.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>", r"<xyz>ABC</xyz>\1<xyz>\2</xyz>", data))
Neither truncate nor open(..., 'w') will change the inode number of the file (I tested twice, once with Ubuntu 12.04 NFS and once with ext4).
By the way, this is not really related to Python. The interpreter calls the corresponding low level API. The method truncate() works the same in the C programming language: See http://man7.org/linux/man-pages/man2/truncate.2.html
file='path/test.xml'
with open(file, 'w') as filetowrite:
filetowrite.write('new content')
Open the file in 'w' mode, you will be able to replace its current text save the file with new contents.
Using truncate(), the solution could be
import re
#open the xml file for reading:
with open('path/test.xml','r+') as f:
#convert to string:
data = f.read()
f.seek(0)
f.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>",data))
f.truncate()
import os#must import this library
if os.path.exists('TwitterDB.csv'):
os.remove('TwitterDB.csv') #this deletes the file
else:
print("The file does not exist")#add this to prevent errors
I had a similar problem, and instead of overwriting my existing file using the different 'modes', I just deleted the file before using it again, so that it would be as if I was appending to a new file on each run of my code.
See from How to Replace String in File works in a simple way and is an answer that works with replace
fin = open("data.txt", "rt")
fout = open("out.txt", "wt")
for line in fin:
fout.write(line.replace('pyton', 'python'))
fin.close()
fout.close()
in my case the following code did the trick
with open("output.json", "w+") as outfile: #using w+ mode to create file if it not exists. and overwrite the existing content
json.dump(result_plot, outfile)
Using python3 pathlib library:
import re
from pathlib import Path
import shutil
shutil.copy2("/tmp/test.xml", "/tmp/test.xml.bak") # create backup
filepath = Path("/tmp/test.xml")
content = filepath.read_text()
filepath.write_text(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>", content))
Similar method using different approach to backups:
from pathlib import Path
filepath = Path("/tmp/test.xml")
filepath.rename(filepath.with_suffix('.bak')) # different approach to backups
content = filepath.read_text()
filepath.write_text(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>", content))

Categories

Resources