Fortran 90 - I/O passing variable as filepath - io

This seems like it should be an easy fix, but after searching for hours I cannot find a solution.
I want to save a filepath as a character variable, then use the variable in a I/O statement. Below is my code for reading from the file defined by "filepath".
character:: filepath
filepath='c:\users\chris\...\data.txt'
open(unit=1,file=filepath,status='old',action='read',form='formatted',iostat=ierror)
if(ierror.NE.0)then
print*,'file cannot be opened or does not exist'
read*,
endif
......
The program compiles (gfortran compiler using NetBeans IDE), but when I run, I get the error printed to the screen.
Note 1: When I put the filepath directly into the open function as shown below, it seems to work fine
open(unit=1,file='c:\\Users\\Chris\\...\\data.txt',status='old',action='read',form='formatted',iostat=ierror)
Note 2: I have tried all variations of forward/backward, single/double slashes in the filepath variable, so I don't think that is the problem. When I print out "filepath" to the screen as a debugging mechanism (before the open function above), all is get back is "C". So for some reason, it seems I am losing the rest of the filepath. (I will leave my questions regarding what slash method is appropriate until I research it more)
I appreciate any suggestions.
Thanks,
Chris

You've declared filepath to be a character variable of length 1. Change the declaration to something like character(80):: filepath (use a length long enough to contain the entire path).

Related

FileNotFoundError in JupyterLab (https://jupyter.org/try-jupyter/lab/)

Has anybody faced a FileNotFoundError in JupyterLab (https://jupyter.org/try-jupyter/lab/) when trying to read a .csv file? The file was uploaded in the same directory. I have used a CopyPath option to be sure that the path is correct. Still not working. Any suggestions on how can I fix this?
here is a snapshot of a simple code and an error
This is unlikely to be an error with Jupyter.
Make sure that the ReadFile.ipynb and theFile.csv are in the same directory. If they are not, use a relative import.
If this still doesn't work, try the following synthax. This is independent of the path you provide.
pd.read_csv(r'C:\Users\aiLab\Desktop\example.csv')
Here r is a special character and means raw string. So prefix it to your string literal.
https://www.journaldev.com/23598/python-raw-string:
Python raw string is created by prefixing a string literal with ‘r’ or
‘R’. Python raw string treats backslash () as a literal character.
This is useful when we want to have a string that contains backslash
and don’t want it to be treated as an escape character.
See: pandas.read_csv FileNotFoundError: File b'\xe2\x80\xaa<etc>' despite correct path
Also, make sure that you don't have unwittingly copied an invisible character.
I ran into the same problem.
Make sure your working directory is the same as your notebook and .csv file. Check this by running pwd. You can run ls to get the list of files in the working directory.
If this helps you find the problem, try shutting down the running kernel completely and attaching a new one. This will change the working directory.

How can Python be forced to use raw string equivalent of variable-stored paths on Windows?

It might seem that this question has been asked hundreds of times, but reading every variant of it, it's clear it has never been fully answered, at least not in the context I am experiencing.
I have a filename variable that is being obtained through a dialog (in Blender), and I need to both use the file name and iterate over its directory. The problem is that Python cannot properly convert the backslashes to forward slashes.
Here is the filename: 'D:\scans\testing\2021_12_01_14_41_38\frame_00000.json'
Storing this in a variable yields 'D:\scans\testing\x821_12_01_14_41_38\x0crame_00000.json'.
In other words, once the dialog passes the filename to the variable, nothing more can be done with it. The file itself may be opened, but attempting any other operation on it automatically converts the escape characters.
Here are some other approaches I have tried:
Attempting a find replace using filename.replace('\\','/') yields 'D:/scans\testing\x821_12_01_14_41_38\x0crame_00000.json'.
Using pathlib.Path(filename) yields a WindowsPath object:
WindowsPath('D:/scans\testing\x821_12_01_14_41_38\x0crame_00000.json')
All I need is the directory and the file separated, but even os.path.basename yields
'testing\x821_12_01_14_41_38\x0crame_00000.json'.
Even trying repr(filename) is to no avail. It yields "'D:\\scans\\testing\x821_12_01_14_41_38\x0crame_00000.json'"
re.sub('\\\\','/',filename) yields 'D:/scans\testing\x821_12_01_14_41_38\x0crame_00000.json'
It's mind boggling that such a simple operation on Windows is so complicated, as I have done it millions of times on Linux (yes, I know). Unfortunately, I cannot use the raw string method (r'string') because this is a variable, not a string. I have seen crazy ideas out there such as r'{}'.format(variable), but that doesn't work for obvious reasons.
I could list hundreds of other failed attempts, including abspath, relpath, and find / replace, and they all lead nowhere. Surely, there is a way to take a full-path filename from a dialog in Windows (in this case, Blender) and split the directory and filename apart?
If you have any ideas how I might work around this problem, please share.
You can try removing the inverted commas form the string while using the variable which has the string stored in it.
I was trying to find file size where file path was chosen by user:
import os
take input on file path
file_path = input("Enter file path without inverted commas:")
prints the size of the file in bytes
print(os.path.getsize(file_path))
Note:
When I copied the path it was copied like this:
"D:\Dev\repo\t1_old\task.py"
So I had to remove the inverted commas, only then the os.path.getsize(file_path) worked.
If I did not remove inverted commas while entering the file path, it gives an error

Removing all special characters from a string that cannot be used inside the command line (python)

I'm making a tk application in which I have to execute a command line application (MP4Box) from python, using subprocess.check_call().
The filename (used in the command) is retrieved from youtube and hence has all types of special characters. I want to remove all characters that can't be used as filenames in the Operating system also btw (i want the implementation would work across multiple platforms)
I tried the solution over here
. windows seems accept the filename but but it returns an error with subproces.check_call().
I tried manually removing the special char from filename and it works good after that. so it isnt a problem with the command.
EDIT:
For eg, i tested for this video. The solution in above link won't remove 'ä' and cause a problem in the command line.
I eventually found the correct answer. Ascii did the trick :)
''.join([t for t in <filenmae> if t.isalnum()]).encode('ascii', 'ignore'))

VBA string comparison failure

i met interesting issue when im comparing two strings. Im reading data from file and everything works well. But then co-worker send me input file, which is just CTRL+C and CTRL+V of working file. And then miracle happend! VBA is so confused, that cant compare two simple strings and i fell of chair.
If you take a look at image you can see that comparison passed if condition where are two same strings, but it should not. Im a bit confused how this can happen.
So met someone something like this? Im realy start thinking about something like machine revolution from Terminator. (files are both saved in notepad++ and there are no strange characters or something like that)
Progress update
So i tried hints from guys in comments below. and ended with something like this
If CStr(Trim(rowArray(4))) <> (CStr("N/A")) Then
Contentent of rowArray(4) is still "N/A" string as on picture above and excel still thinks this strings arent same. I also saved file in pspad, netbeans, and normal notepad and issue is still same.
Use the immediate window to test the contents of the variable:
For i = 1 To Len(rowArray(4)): Print Asc(Mid(rowArray(4), i, 1)): Next
This will print the ASCII value of each character in the string - you can use this to determine what the extra character(s) are causing the issue.

VBA how to run a string as a line of code

Is there a way to convert a string into an executable line of code?
something like:
Dim Line1 as String
Line1 = "MsgBox (""Hello"")"
Execute Line1
resulting in the pop up box saying Hello.
What I'm doing is pulling lines out of a text file. I can pull lines that are the names of form controls and use them to perform actions on those form controls but I'd like to go one step further and execute lines.
I've seen claims that this would work:
Application.Run Line1
or make the variable an array and store it in element 1 e.g. and use
Application.Run Line1(1)
but it doesn't work for me.
Ok, while writing this I've also been experimenting. I found that
Eval (Line1)
will work when Line1 is a message box, but not when it is something like:
line1 = "DoCmd.OpenForm ""form1"""
Any tips would be appreciated.
Thanks
You can use,
Eval("DoCmd.OpenForm(""form1"")")
You have to make sure any functions you include use parentheses.
Further reference,
http://msdn.microsoft.com/en-us/library/office/aa172212(v=office.11).aspx
It's not exactly what I was asking, I ended up going a slightly different direction, but here's what I ended up doing and it would probably be easily altered to more closely match my question. I actually took lines of text from an external text file and inserted them into a line of code. What I was doing in this example was just hiding columns, the external text file was a list of column names. (figuring out how to output that was fun too)
Open "C:\UserList.txt" For Input As #TextFile
While Not EOF(TextFile)
Line Input #TextFile, TextLine
Screen.ActiveDatasheet.Controls(TextLine).ColumnHidden = True
Wend
Visual Basic is a compiler language, and as such does not support the ability to execute human-readable code while it is running. All code written in VBA must first be compiled BEFORE the program runs the first time.
However, SQL is an interpreter language, and can be handed code which it will execute line by line. You can also fetch contents for variables from other sources while the program runs, you just can't build a string while the program is running and then execute it in VBA directly.

Resources