Python copy and paste path file from Windows Explorer into variable - python-3.x

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.

Related

.txt file is opening but prints nothing

I'm trying to open a text file and print it as a string. I've made sure there is text in the .txt file but when I run the code it just prints an empty space, I don't know what to do at this point since I couldn't find anything that could help me with my problem.
with open('test.txt', 'r') as file:
data = file.read().rstrip()
print(data)
When things aren't opening check the following:
You wrote exactly the same in your code as the one you saved. "file.txt" is not the same as "File.txt" for Python (same goes for accents and special characters).
The file you are trying to read is in the same directory. If your code is at users/bla/documents/another_folder and you just pass the name of the file to your code, then the file must be at users/bla/documents/another_folder too. If not, be shure to add it into the string path as "path/to/your/file/file.txt"
Make sure that the extension .txt is the same as your file.
If you checked that but everything seems correct, try:
with open(path_to_file) as f:
contents = f.readlines()
And see if "contents" has something.
I think it is better if you use open("file.txt","r") function to do it. So your code will be like this:
file=open("test.txt","r")
data=file.read().strip()
print(data)

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 to open a text file from my desktop while using python 3.7.1 in Terminal

I saved a text file to my desktop named "test.txt" within the file I wrote only my name, David. Then, I opened terminal and opened python 3.7.1 and wrote the following code in attempt to see my name, David, populate:
open("/Users/David/Desktop/test.txt,"r")
However, I receive the following error message:
SyntaxError: EOL while scanning string literal
Does anyone know how I can avoid this error and have my name, David, read from the test.txt file on my desktop? Or am I going about this completely wrong?
As #Matt explained, you are missing quotes.
You can follow below approach to open file and read from it.
myfile = open("/Users/David/Desktop/test.txt","r") #returns file handle
myfile.read() # reading from the file
myfile.close() # closing the file handle, to release the resources.
For more information on how to do read/write operations on file
You are missing a quotation mark, after your file path. It should look like this:
open("/Users/David/Desktop/test.txt","r")
^ This quotation mark
This will open the file correctly, however you will still need to actually read from it.
You are missing the other quotations as the others have mentioned. Try using the with open statement, as it handles your resources for you, meaning you don't need to specify .close()
with open("/Users/David/Desktop/test.txt", "r") as file:
file.read()
you can use with which will close the file automatically as you come out of the block and put your Directory link
with open(r"Directory_link", "r") as file1:
FileContent = file1.read()
print(FileContent)

How to find the file path of a file in livecode

So, I'm incredibly new to LiveCode and I have an external file in the same directory as the .livecode file called 'words.txt', with english words, each on a new line. I plan to read this file into a variable and then pick a random word from that variable. However, I am stumped as to how I must find the file path and insert this into the syntax required for me to do this. My code is as follows:
put url ("binfile:" & filePathGoesHere) into dictionary
replace crlf with lf in dictionary
replace numToChar(13) with lf in dictionary
put any line of dictionary into randomword
The file path is supposed to be inserted into the code at filePathGoesHere. Once the program is compiled I will be moving it and its resources around a bit (from computer to computer), so, beyond the text file staying in the same folder as the compiled program, the file path will change. What extra code would I need to add to make this work, if the folder the compiled program and the txt file is in is called "MyProgram"?
Help is much appreciated, and if further specification is required I can provide it. I also have a folder called "resources" if moving it there can help.
If the stack you're building is for your own use, you can place external files anywhere, but if you're going to deliver your stack to other users, you need plan where you external files are going to be placed, and how.
An easy way to determine the path to a file that sits immediately outside your stack is using the stack's filename:
put the fileName of this stack into theFilePath
set the itemDel to "/"
put "words.txt" into the last item of theFilePath
Now theFilePath variable will an absolute path reference to your external file. If the file is placed inside a folder "TextFiles" you can do this:
put the fileName of this stack into theFilePath
set the itemDel to "/"
put "TextFiles/words.txt" into the last item of theFilePath
If you're going to deliver your stack to other people, you should write your external file/s into a common system folder, or you need to use an installer to define where your files/folders will be placed. Common folder paths are found using the specialFolderPath function:
put specialFolderPath("Documents") into the theFolderPath
A somewhat recent addition to LiveCode is a "Resources" folder -- specialFolderPath("Resources") -- which can be handy for delivering on desktop and mobile platforms. Also, keep in mind that few of these folders allow writing to existing files contained in them for security reasons. "Preferences" and "Documents" are two examples of folders where you can change the contents of files.
The LC dictionary contains details of each of the folders.
If you use the file: scheme instead of bindle: LiveCode will automatically convert end of line characters to LF, so that step may not be necessary. (Although you might need it if you are reading a text file produced in native Windows encoding on a Mac.) You don't even necessarily need to read it into a variable. You could do this:
put any line of URL ("file:" & specialFolderPath("resources") & "/words.txt") \
into tRandomWord

String Manipulation in Bash when variable is a path

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

Resources