How to Print a Path in Python? - python

I want to make a script open the CMD and then enter a path:
import pyautogui as pag
pag.hotkey('win','r')
pag.typewrite('cmd')
pag.press('enter')
pag.typewrite('C:\Users\XY\AppData\')
that doesn't work. So, I tried this:
import pyautogui as pag
pag.hotkey('win','r')
pag.typewrite('cmd')
pag.press('enter')
pag.typewrite('C:\\Users\\huba5_000\\AppData\\')
However, this entered C:?Users?XY?AppData?
What I want it to enter is C:\Users\XY\AppData\. Do you know what I should write instead of '\\' ?
Thank you in advance!

when a string is read in from input() or from text boxes in gui's (in general.. idk about pag) the extra slashes are automatically put in. They are not automatically put in for string literals in your code however and must be escaped (hence the double slash). Here is a short console session (python 2.7) showing that functionality:
>>> s = raw_input('enter a path: ') #change raw_input to input() for python 3.x
enter a path: \usr\var
>>> s
'\\usr\\var'
>>> print s
\usr\var
notice when I entered the path, I did not escape my backslashes, yet when I call the internal representation of s they have been put in for me. when I want the output format, I call print to execute any formatting (escapes) contained within the string

Related

How to display on the screen a different character from the pressed one with Python?

I'm trying to write a program that works like the website https://www.peteranswers.com/. That is, to display a character on the screen that is part of a previously written text, whichever is the character you type. My attempt is this:
f=open("text1.txt", "r")
g=open("text2.txt", "w")
while True:
a = input()
g.write(a)
c = f.read(1)
if not c or a == "$":
break
print (c)
f.close()
g.close()
It works, but I would like not to display the characters you type and not to have to press enter each time.
How could this be done? Does it exist a more straightforward way to accomplish this task?
I'm working on Python 3.7 and IDLE.
The site you link does not work for me. From the code, I presume that you want something that works like a password entry function. But you want the characters echoed to be taken from an existing text instead of being nothing or a stream of ' 's or '*'s.
If so, I recommend that you modify the appropriate function, for Unix or Windows, in the getpass module. Note that both echo suppressing functions require that sys.stdout == sys.stdout (== system terminal and not None). Neither echo anything, you would have to add that. Neither work when you run in an IDE, such as IDLE, that rebind sys.stdout to print to a GUI.
If you are on Windows, you should read https://docs.python.org/3/library/msvcrt.html#console-i-o. You would use putch to write bytes, or use both getwch and putwch to input and output unicode characters. On Unix, you will have to dig into the code yourself.

Pass a variable to read_csv as an argument?

To be more user friendly, I have created a prompt for the user to input a file path.
print('Please enter file path surrounded by quotes.')
path = 'r' + input()
df = pd.DataFrame(pd.read_csv(path, index_col=False))
When input is
"C:\path\somefile.csv"
Output returns
FileNotFoundError: [Errno 2] File b'r"C:\path\somefile.csv"' does not exist: b'r"C:\path\somefile.csv"'
If I remove the variable input and drop the file directly into the read_csv argument, it works just fine. Can someone educate me?
You were over-engineering your code. It's as simple as this:
print('Please enter file path without quotes.')
path = input()
df =pd.read_csv(path, index_col=False)
You don't need the quotes, as they will be added automatically
I think you are confusing raw string literals with a string value. When we write something like
csv = pd.read_csv(r'C:\path\somefile.csv')
the r'C:\path\somefile.csv' is a string literal. This is how we represent the string in python code. The user should never be aware of the r'' notation, including both the letter r and the quotes. Instead, they should just type the path as they see it in other programs. This means that you can just do
path = input()
Also the user shouldn't be required to type any quotes.

How to enter a long and not-a-string data into the argument?

I have a problem with Python and need your help.
Take a look at this code:
import os
os.chdir('''C:\\Users\\Admin\\Desktop\\Automate_the_Boring_Stuff_onlimematerials_v.2\\automate_online-materials\\example.xlsx''')
The os.chdir() did not work because the directory I put in between the ''' and ''' is considered as raw string. Note that a line is no more than 125 characters, so I have to make a new line.
So how can I fix this?
You can split your statement into multiple lines by using the backslash \ to indicate that a statement is continued on the new line.
message = 'This message will not generate an error because \
it was split by using the backslash on your \
keyboard'
print(message)
Output
This message will not generate an error because it was split by using the backslash on your keyboard
Lines can be longer than 125 characters, but you should probably avoid that. You have a few solutions:
x = ('hi'
'there')
# x is now the string "hithere"
os.chdir('hi'
'there') # does a similar thing (os.chdir('hithere'))
You could also set up a variable:
root_path = "C:\\Users\\Admin\\Desktop"
filepath = "other\\directories" # why not just rename it though
os.chdir(os.path.join(root_path, filepath))
Do these work for you?
I'm also curious why you have to chdir there; if it's possible, you should just run the python script from that directory.

Python: change "\" to "\\"

I have an entry field that allows the user to enter their own directory/path structure when saving a file, if the user just copies their directory from windows explorer you can get text like "c:\directory\file.ext" how can I take text entered like that and replace it with the necessary "\" for the os module to use?
Use a raw string to get the backslashes
>>> path = r"c:\test\123"
>>> path
'c:\\test\\123'
>>> print path
c:\test\123
maybe os.path.normpath() would be helpful to you - and the documentation here

Eclipse/PyDev treats newlines pasted into its console as instructions, but I want it to parse them as part of a long string

I am working on a Python script to automate some repetitive text-fiddling tasks I need to do. I use PyDev as a plugin for Eclipse as my IDE.
I need the script to accept user input pasted from the clipboard. The input will typically be many lines long, with many newline characters included.
I currently have the script asking for input as follows:
oldTableString = raw_input('Paste text of old table here:\n')
The console correctly displays the prompt and waits for user input. However, once I paste text into the console, it appears to interpret any newline characters in the pasted text as presses of the enter button, and executes the code as if the only input it received was the first line of the pasted text (before the first newline character), followed by a press of the enter key (which it interprets as a cue that I'm done giving it input).
I've confirmed that it's only reading the first line of the input via the following line:
print oldTableString
...which, as expected, prints out only the first line of whatever I paste into the console.
How can I get Eclipse to recognize that I want it to parse the entirety of what I paste into the console, newlines included, as a single string?
Thanks!
text = ""
tmp = raw_input("Enter text:\n")
while tmp != "":
text += tmp + "\n"
tmp = raw_input()
print text
This works but you have to press enter one more time.
What about reading directly from the clipboard or looping over every line until it receives a termination symbol or times out. Also, is it important to make it work under Eclipse? Does it work when executed directly?

Categories

Resources