dispose syntax error when using fortran open statement - file

I am opening a file which has to be deleted at the end. The following command complains about using dispose.
f = "espy.tmp"; h = "formatted"; r = "read"
Open (newunit=u, file=f, form=h, action=r, &
status="old", dispose="delete")
lib/core.f:177:21:
status="old", dispose="delete")
1
Error: Syntax error in OPEN statement at (1)

Dispose is a non-standard compiler extension (and not supported by your compiler). As described in this answer, the standard way to do this is to delete the file on closure:
f = "espy.tmp"; h = "formatted"; r = "read"
Open (newunit=u, file=f, form=h, action=r, &
status="old")
close(u, status='delete')
Or, you could use temporary/scratch files (no filename):
f = "espy.tmp"; h = "formatted"; r = "read"
Open (newunit=u, form=h, action=r, &
status="old", status='scratch')

Related

Lua Script error: bad argument #2 'to open' (invalid mode)

I am trying to create a file or read a already existing file but I keep getting the same error: bad argument #2 'to open' (invalid mode)
player_pos = player.pposition()
world_nam = server.worldName()
attribs = player.attributes()
xp = attribs.id(6).value
timesavename = world_nam .. player_pos .. xp
f = io.open(timesavename, ".txt", "r")
if f ~= nil then
io.input(f)
resultstart = f:read("*line")
resultstop = f:read("*line")
f:close()
end
Using , in a function will make it think its another argument, like in the comment you got you should concat the strings with ..

winerror32 python renaming file

Im using FTP connection, after closing connection, i need to work with file. How can I do that?
import ftplib, os, time
host = "ftp_host"
ftp_user = "ftp_user"
ftp_password = "ftp_pass"
filename = "Mon.xlsx"
filename2 = "Monitor9564.xlsx"
os.rename(filename, filename2)
con = ftplib.FTP(host, ftp_user, ftp_password)
f = open(filename2, "rb")
send = con.storbinary("STOR " + filename2, f)
con.close
time.sleep(2)
os.rename(filename2, filename)
But im getting an error
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Monitor9564.xlsx' -> 'Mon.xlsx'
You didn't close your file handle before attempting to rename your file. You can issue f.close() before your second rename method, or you can let Python deal with proper file handle scopes for you:
# code...
with open(filename2, "rb") as f:
send = con.storbinary("STOR " + filename2, f)
con.close()
os.rename(filename2, filename)
Also, why are you renaming your file only to rename it back to its original name?

File Open Mechanism

I'm trying to create an application. The application gives the user 2 combo boxes. Combo Box 1 gives the first part of the file name the user wants, and Combo Box 2 gives the second part of the file name. E.g. Combo box 1 option 1 is 1 and Combo Box 2 option 1 is A; the selected file is 1_A.txt.
I have a load button which is to use the file name , and open a file with that name. If no file exists, the application opens a dialog saying "No Such File Exists"
from PySide import QtGui, QtCore
from PySide.QtCore import*
from PySide.QtGui import*
class MainWindow(QtGui.QMainWindow):
def __init__(self,):
QtGui.QMainWindow.__init__(self)
QtGui.QApplication.setStyle('cleanlooks')
#PushButtons
load_button = QPushButton('Load',self)
load_button.move(310,280)
run_Button = QPushButton("Run", self)
run_Button.move(10,340)
stop_Button = QPushButton("Stop", self)
stop_Button.move(245,340)
#ComboBoxes
#Option1
o1 = QComboBox(self)
l1 = QLabel(self)
l1.setText('Option 1:')
l1.setFixedSize(170, 20)
l1.move(10,230)
o1.move(200, 220)
o1.setFixedSize(100, 40)
o1.insertItem(0,'')
o1.insertItem(1,'A')
o1.insertItem(2,'B')
o1.insertItem(3,'test')
#Option2
o2 = QComboBox(self)
l2 = QLabel(self)
l2.setText('Option 2:')
l2.setFixedSize(200, 20)
l2.move(10,290)
o2.move(200,280)
o2.setFixedSize(100, 40)
o2.insertItem(0,'')
o2.insertItem(1,'1')
o2.insertItem(2,'2')
o2.insertItem(3,'100')
self.fileName = QLabel(self)
self.fileName.setText("Select Options")
o1.activated.connect(lambda: self.fileName.setText(o1.currentText() + '_' + o2.currentText() + '.txt'))
o2.activated.connect(lambda: self.fileName.setText(o1.currentText() + '_' + o2.currentText() + '.txt'))
load_button.clicked.connect(self.fileHandle)
def fileHandle(self):
file = QFile(str(self.fileName.text()))
open(file, 'r')
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.setWindowTitle("Test11")
window.resize(480, 640)
window.show()
sys.exit(app.exec_())
The error I'm getting is TypeError: invalid file: <PySide.QtCore.QFile object at 0x031382B0> and I suspect this is because the string described in the file handle isn't being inserted in the QFile properly. Can someone please help
The Python open() function doesn't have any knowledge of objects of type QFile. I doubt you actually need to construct a QFile object though.
Instead, just open the file directly via open(self.fileName.text(), 'r'). Preferably, you would do:
with open(self.fileName.text(), 'r') as myfile:
# do stuff with the file
unless you need to keep the file open for a long period of time
I came up with a solution also.
def fileHandle(self):
string = str(self.filename.text())
file = QFile()
file.setFileName(string)
file.open(QIODevice.ReadOnly)
print(file.exists())
line = file.readLine()
print(line)
What this does is that it takes the string of the filename field. Creates the file object. Names the file object the string, and then opens the file. I have exists to check if the file is there, and after reading the test document i have, ti seemed to work as I wanted.
Thanks anyway #three_pineapples, but I'm going to use my solution :P

open(create) non existing file in lua

I'm trying to save a configtable(parsed to string) to a file wich is created just in time.
local cfg_string = table.tostring(cfg_table)
local file_name = ""
local cfg_file = ""
file_name = com:line(nil) -- reads a line of input from user via terminal
file_name = string.format("some_prefix-%s-some_suffix.lua",file_name)
-- file does NOT exist at this line
cfg_file = io.open("/dir/subdir/"..file_name,"w")
-- file now should exist
os.syslog(type(file_name)) -> string
os.syslog(type(cfg_file)) -> nil
os.syslog(type(cfg_string)) -> string
cfg_file:write(cfg_string)
cfg_file:write(cfg_string) throws "attempt to call "write" a nil value".
So hu,.. cfg_file is nil I know, but why? I also tried to io.open() with "a" flag, but this doesn't work too. The directory exists!
Thanks for your help.
Actually, my code is working. The error is thrown because missig write-permission to dir.
Everyone with such Errors should try
handle_name, err = io.open(file,"w")
print(err)

TypeError: invalid file: When trying to make a file name a variable

Hi I am trying to represent a file location as a variable because the finial script will be run on another machine. This is the code I have tried followed by the error I get. It seems to me that some how python is adding "\" and that is causing the problem. If that is the case how do I get it not to insert the "\"? Thank you
F = 'C:\Documents and Settings\myfile.txt','r'
f = open(F)
and the error
TypeError: invalid file: ('C:\\Documents and Settings\\myfile.txt', 'r')
From the docs:
http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
Try this:
F = r'C:\Documents and Settings\myfile.txt'
f = open(F, 'r')
About the "double backslashes" - you need to escape backslashes in your strings or use r'string', see this:
http://docs.python.org/release/2.5.2/ref/strings.html
E.g. try this:
>>> a = 'a\nb'
>>> print a
a
b
To get what you expect, you need this:
>>> a = r'a\nb'
>>> print a
a\nb
or this:
>>> a = 'a\\nb'
>>> print a
a\nb
Try
f=open('C:\Documents and Settings\myfile.txt','r')
Instead of using the variable F. the way you have it 'r' is part of the file name, which it is not.
Instead of writing / or \, you should do this:
import os
F = os.path.join(
"C:",
os.path.join(
"Documents and Settings", "myfile.txt"
)
)
f = open(F, 'r')`
so that it uses / or \ according to your os.
(Although if you write C:/ it must mean you want to run your code on Windows... Hum...)

Resources