Am I changing directories properly in Python? - python

I'm trying to figure out how to compile a github project with Python. I imported my os, but I am getting a syntax error when I attempt to change directories with this code: os.chdir(C:\Users\User\Desktop\Folder)
After doing that, I get this:
>>> os.chdir(C:\Users\User\Desktop\Folder)
File "<stdin>", line 1
os.chdir(C:\Users\User\Desktop\Folder)
^
SyntaxError: invalid syntax
I see that it is pointing at the colon. Am I putting in the directory incorrectly? (I have never used python in my life.) Any help would be greatly appreciated. Thanks in advance!

You need to pass it a string. And because it's a Windows path, it should be a raw string (Quote mark prefixed with r, like r''), so the backslashes don't get interpreted as string literal escapes (raw strings are more succinct than the alternative of doubling all the backslashes), making it:
os.chdir(r'C:\Users\User\Desktop\Folder')

Related

Python Converts the backslash followed by numbers into Unicode

I have a string which is a Windows path returned by another function. The function returns the path with a single backslash within it. Here I can't using raw string conversion to a variable. re.escape(path) does not work either. path.replace('\','\\') throws SyntaxError: unexpected character after line continuation character
The function returns a path something like "D:\Data\201909\Foo\20190927c\Files" which gets coverted into "D:\\Data\ΓΌ909\\Foo\x8190927c\\Files"
path can be assumed as the variable containing the value returned by the function.
Could you please suggest me a solution for this.
Thanks Much !
The below solution worked for me.
path = r"{}" .format(path)
where new variable path is the converted raw string.

Syntax error RunPython using xlwings, sys.path

OS Windows 10 Pro
Versions of xlwings, Excel, and Python (0.9.0, Office 365, Python 3.8.2)
I am new on using xlwings through VBA. I run the exact syntax from a tutorial webpage on both VBA and Python, but it gives error like this:
File "<string>", line 1
import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars(r'C:\Users\User\Trial2;C:\Users\User\Trial2\Trial2.zip;C:\Users\User\Anaconda3\')).split(';'); import Trial2;Trial2.main()
SyntaxError: invalid syntax
I used original syntax for VBA, and the syntax I used for python is like this:
import xlwings as xw
##xw.sub # only required if you want to import it or run it via UDF Server
def main():
wb = xw.Book.caller()
wb.sheets[0].range("A1").value = "Hello xlwings!"
##xw.func
def hello(name):
return "hello {0}".format(name)
if __name__ == "__main__":
xw.Book("Trial2.xlsm").set_mock_caller()
main()
I barely find any clue for this problem, so I'm hoping that someone can give me a solution
I realize this is a long time after the initial question but I had the same issue and couldn't find an answer anywhere. After playing around (for much longer than I care to admit) I found the problem for me was that my .xlsm/.py file names contained a space. With no other changes, everything worked when I replaced the space with an underscore.
This is a quirk in python's string literals. Even with raw strings the backslash escapes the quote character so r"ends in quote\"" is valid. It also means that raw strings can't end in a single backslash. r"ends in slash\" is a syntax error. If you need to end a string with a backslash, you can't use raw. "ends in slash\\" is okay.
I'm not sure where the failing string comes from, but you need to change it to
import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars('C:\\Users\\User\\Trial2;C:\\Users\\User\\Trial2\\Trial2.zip;C:\\Users\\User\\Anaconda3\\')).split(';'); import Trial2;Trial2.main()
See Python Lexical Analysis
Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character).

pushing a list to a file in python

I have a list like this
dis=('a','b','c',100)
I want it to push to a .Csv file(plan_to_prod2) ,but my folder name is a integer
my_df = pd.DataFrame(dis)
my_df.to_csv('E:\23\4\plan_to_prod2.csv')
i am getting invalid file name as error even though my file name is correct
You should use a raw string literal.
A \ followed by an integer is interpreted as a unicode character which is an invalid file name. Try print('E:\23\4\plan_to_prod2.csv') and see the output (I would have pasted it here but these characters don't show up when the answer is rendered). You can also see the problem in the error you provided in the comment.
When using raw string:
print(r'E:\23\4\plan_to_prod2.csv')
# E:\23\4\plan_to_prod2.csv
Instead of using raw string you can also use double slashes, ie print('E:\\23\\4\\plan_to_prod2.csv') but I find using raw strings much easier.
The \ character is used for escapes. So when you try to find the path you escape.
You should use / or use raw string r'' instead of \. Also, you could escape those backslashes by escaping it with an additional \.Choose whichever suits you best.
r'E:\23\4\plan_to_prod2.csv'
'E:\\23\\4\\plan_to_prod2.csv'
'E:/23/4/plan_to_prod2.csv'

I want to call my bingo.sh file through python in mac how to do that?

os.system('sh ~/scripts/bingo.sh')
this gives an error of Non ascii characters.
import subprocess
subprocess.call(["sh","/full/path/bingo.sh"])
"Python 2 uses ascii as the default encoding for source files, which means you must specify another encoding at the top of the file to use non-ascii unicode characters in literals. Python 3 uses utf-8 as the default encoding for source files, so this is less of an issue"
I was able to find this by googling "Python error 'Non ascii characters'"
From my understanding this is a good answer
How to make the python interpreter correctly handle non-ASCII characters in string operations?
hope this helps
(I am new to coding and was looking into this to help my own understanding of how to avoid this problem for myself. Take what I have to say with a grain of salt.)

file reading / reference issue in python

Recently I have referenced a file in my desktop using python 3.4 64bit GUI application. The problem I have got is as follows:
The code I tried is :
fo=open("c:\users\Ismail Nuru\Sesktop\myfile\lab.txt","r+")
string1=fo.read()
print(string1)
fo.close()
Python is trying to use the \uXXXX part of your string as a unicode escape sequence.
To fix this, you have 3 options here:
Use a raw string r'C:\users\Ismail Nuru\Sesktop\myfile\lab.txt'
Double the backslashes 'C:\\users\\Ismail Nuru\\Sesktop\\myfile\\lab.txt'
Or use forward slashes 'C:/users/Ismail Nuru/Sesktop/myfile/lab.txt')

Categories