This may be an easy question for someone more experienced than me out there. Any help would be massively appreciated.
I'm trying to manipulate a text string using the below code:
CONTENTS=${RESPONSEFILE%</Header></StockResponse>}
Where RESPONSEFILE = /apps/live/awards/tmp/NexPhase4.tmp
Trouble is whenever I run the above code it takes the pathname of the file and tries to manipulate that instead of opening up the file and manipulating the text within the file.
that is exactly what bash string manipulation is supposed to do: http://tldp.org/LDP/abs/html/string-manipulation.html
what you want can be done using sed or other programs that allow to change files
Related
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
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.
I developed a software in Python3.4 which has as input a config file with a set of variables and some of them are paths.
I have a problem with the separator in the file path name.
I know I can put a "r" in front of the string or use the double backslash or the slash so to have a raw string, but because this software will be used by other users and the average one is just a "copy and paste" guy, I don't want they manipulate the file name.
So the users have just to copy from the Windows Explorer the file path and paste into the config file, something like:
path_variable = "C:\Users\home\room\table.txt"
and I want to write a function that modify it so can be used.
How can I do it? If I leave the string in this way I obtain an unicode error because of \U...
Thanks a lot,
Ciccio
Update
The config file is not a python file but just a .txt file within the variables and their values:
var_name = var_val
path_variable = "C:\Users\home\room\table.txt"
height = 20
plot_write_variable = ["y", "n"]
This is just a temporary solution until I finalize the GUI. This file must be really easy to understand and the software has to be used by people don't have any knowledge of Python or programming. The final user has just to change the variables value and click on the executable file to run the program and nothing else. For this reason I want to avoid as much as possible the use of any python command.
To read the config file I use this function:
import imp
def read_inputFile(path):
file = open(path)
variables = imp.load_source('data', '', file)
file.close()
return variables
Your config files don't have to be written in Python. You could use configparser, which so far as I can tell does not interpret backslashes specially.
This may require some re-working of your logic for the config file. Instead of importing the config file directly with an import statement, you would parse it with the configparser module. You may also need to adjust the syntax of your config file to match what configparser is expecting.
You say that your users are "copy and paste" kind of guys, but you are having them paste file locations into a config file. I don't know your code, but if the config file is in python, you could add a line:
path_variable = input("Enter the path").replace("\\", "\\\\")
Python interprets \\ as \, which fixes your problem
This solves the problem of them copy-pasting into the file, and instead prompts them for the filepath.
A while ago I had compressed an application using Linux "tar -cf" command. At that time some of the file names were in a different language.
Now when I uncompress using "tar -xf" it shows the file names in the other language as question marks.
Is there a way that when I uncompress it keeps the original file names as they were?
Your help is highly appreciated.
Good question ! It's expected that like any Unix command, tar could pipe its output to another program, if possible including filename data. A quick googling reveals that this is the case: as described in this blog post, GNU tar supports the --to-command parameter to write the output to a pipe, instead of directly operating on the directory.
http://osmanov-dev-notes.blogspot.com.br/2010/07/how-to-handle-filename-encodings-in.html
So it's a matter of writing a script to convert the filename to UTF-8, like it's done in the cited post. Another option, also described in the text, that becomes obvious after you read it is to simply extract everything and then write a script to convert every file in the directory. There's a trivial php script in the link that does this.
Finally, you can always write your own custom tar version with the help of scripting languages, and that's easy. Python, for example has the tarfile module built in the standard library:
http://docs.python.org/2/library/tarfile.html#examples
You could use TarFile.extractfile(), shutils.copyfileobj() and str.decode() in a loop to manually extract the files while changing the file name encoding.
References:
http://www.gnu.org/software/tar/manual/tar.html#SEC84
http://docs.python.org/2/library/tarfile.html
http://www.manpagez.com/man/1/iconv/
I am working on a bash prompt project which acts upon different states in the directory you are currently in.
What I need now is a way to load a mini configuration from the current directory, which is easily done with . .params.conf, except that this method is extremely unsecure, as anyone with write access to the directory you are in can create and execute command as you when you stumble upon a directory with a .params.conf file in it.
What is the best way of loading variables from a file like this?
The variables is going to be mostly in a true/false state, so I am not parsing them in any way so it can be executed.
Possible solutions:
A loop, loading each predefined variable is a possible, but I want to keep the code in a readable fashion.
Put the whole file in an bash array would be the best solution, but how can I populate a key/value dict in bash like this?
If bash can source a file only loading the variables..
I dont know how the format of the params.conf file will look like yet. But I think the easiest would be one line for each param, separated by space. Like one of this on each line: key value that can have space in it.
eval can be very insecure and can still execute malicious code. It's better to use declare:
while read varname value
do
declare "$varname=$value"
done < .params.conf
If your values do not contain quotes, it is rather easy:
while read varname value ; do
eval $varname="'$value'"
done < .params.conf
If there are quotes inside of the variables, you have to be more careful and add some escaping.