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

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

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.

Batch replacing unidentified Characters in Unix that were created by macOS

On a Linux volume as part of a NAS with many TB of data some files were created from macOS and some of those files uploaded from macOS seem to include characters in filenames that cannot be reproduced via FTP or SMB file protocol. These files will appear as e.g. "picture_name001.jpg". Where the "" probably stands for a colon or slash.
I can search for "" and found out it applies to 2171 files in distributed locations on the volume. Way too much to manually find and correct each file name.
I thought I can connect to the NAS via SSH and simply loop through each directory doing an automated replace of the "" into "_", but this doesn't work because:
for file in **; do mv -- "$file" "${file///_}"; done
this attempt will throw back an error on the first item matching  with:
mv: can't rename '120422_LAXJFK': No such file or directory
So obviously this substitute character displayed as "" is not the way to address the file or directory as it refers to a name that doesn't actually exists in the volume index.
(A) How do I find out if "120422_LAX:JFK" or "120422_LAX/JFK" is meant here, and (B) how do I escape these invalid characters to eventually be able to automatically rename all those names to for example "120422_LAX_JFK"?
Is there for example a way to get a numerical file ID from the name and then instruct to rename the file by number in case its name contains ""?
I think the problem is that behind this "" can be different codes of symbols. When the system can't represent some characters (for example, given encoding is not supported), then it automatically replaced by some default character (in your case it is ""). But actually there is some code of the character, that should be in the name. BUT when you trying to do this for file in **; do mv -- "$file" "${file///_}"; done system can't recognize code, that symbol is "" is stands for.
I think this problem can be solved by changing the encoding of characters (they should be compatible and better the same) on both devices (mac and NAS)
Hope this would help

String problem when executing a VBScript from within a batch file

I have a batch that I am running at the end of an installer process so that the end-users have as minimal an input as possible.
Everything with the .exe and the batch file are functioning grand. I have got a generic VBScript that performs the function of finding a string and replacing it. I have tested it on a simple string in the batch file and it works fine as below to activate the VBScript and make it perform the find and replacement.
Find_And_Replace.vbs "46233.xml" "DuchessLoco_1" "DuchessLoco_10"
The problem comes that I want the strings to Find and Replace to be far more detailed and it cannot read the characters that I'm typing in to the quotation marks.
I gather this may be because the batch itself understands certain characters that need escaping and then the VBScript will require other ones escaping in a different way. This is where my understanding is letting me down - but I am loathe to make the vbs file more bespoke as it being generic suits the fact I will have multiple different uses for it with multiple different installers over time. Below is an example of the string that I want to Find and modify. The VBScript is executing but not finding hence replacing the string likely due to my errors.
c:"deltaString">DuchessLoco_1</Name>
I have put this in my code as:
"c:\"deltaString\">DuchessLoco_1</Name>"
and this is the string that is failing to be found by the VBScript- I shall later need to move on to using more complex characters such as Tab and New line, so if it isn't possible via this method I will have to rethink my approach to the problem.

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'))

Fortran 90 - I/O passing variable as filepath

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).

Resources