Double backslash in python absolute path [duplicate] - python

This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 7 months ago.
I am new to Python and I am trying to use the 2 lines below.
book = xlrd.open_workbook('C:\files.xls')
sheet = book.sheet_by_name("Sheet2")
but I keep getting the 'c:\\files.xls' file not found. I tried the using (c:/file.xls),(r'C:\files.xls'), and os.path.normpath and still no joy.
Can anyone offer any assistance on how to deal with this? I know why it does python does it but not how to correct it in this context.
Any help would be greatly appreciated.

you can use pandas too acheive the samething
import pandas as pd(pip install pandas to install it)
book=pd.read_excel(r'C:/files.xls)

Related

How can I open a text file with Notepad? [duplicate]

This question already has answers here:
Open a text file using notepad as a help file in python?
(7 answers)
Closed 3 years ago.
Hoping somebody can help me with the following.
The function works just fine:
def fileOpen(filename, accessmode):
file = open(filename, accessmode)
for line in file:
print(line)
return
The filename is "open.txt"
Instead of opening notepad the results are displayed in the python console. Notepad is not opened. I know that it can be solved with this:
import os
os.system('notepad.exe ' + 'open.txt')
But I was wondering if there is a setting in visual studio to get around my problem?
Any help would be greatly appreciated.
VisualStudio Community 2017
Python 3.6.6
Sparkington
The 'problem' is that there is no problem. You are attempting to use the print function to open notepad, not to print to the console - which is its job. This is 'solved' by using os.system() which will execute the notepad.exe program with the argument of 'open.txt'.
So I think you do not need to be looking for a solution to this problem, as there is no problem to be honest with you. You are using print to do something which is not its intended purpose, or even close to it. So is there any wonder that it doesn't work?
My suggestion to you, is to just use os.system and not to try using print for something other than its function.

Get system file type names python [duplicate]

This question already has answers here:
How can I check the extension of a file?
(14 answers)
How to check type of files without extensions? [duplicate]
(10 answers)
Closed 4 years ago.
Is there any way to retrieve a file type name in Python using the OS module?
An example made up command:
>>> os.file_type('txt')
Would return:
'Text Document'
Any help would be appreciated :)
Oscar.
For getting file type you need to check the extension of the file
I think this can help.
import os
if os.path.splitext(file)[1] == ".txt":
pritn 'Text Document'
For os related task you can look over this doc.
https://github.com/Projesh07/Python-basic/blob/master/python_os_module/python_os_module.py

Python: Get script location [duplicate]

This question already has answers here:
How do you properly determine the current script directory?
(16 answers)
Closed 8 years ago.
I've got a question.
How can a program get its own location?
For example, I've got a script ("script.py") on a path ("C:\Programs\script.py").
I want a function, which gives me the path. Like the following:
scriptdic() ==> "C:/programs/"
Thanks for coming answers.
this should do the trick. . .
import os.path
import sys
os.path.dirname(sys.argv[0])

Python: Opening multiple files using 'with open' [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: open multiple files using “with open”?
Does, With open() not works with python 2.6
I have seen other answers to help me with this.
But I can't seem to get any of the solutions provided to work at all!
Here is part of my code for it:
with open('Levels.dat', 'w') as l, open('Names.dat', 'w') as n:
I got this error:
with open('Levels.dat', 'w') as l, open('Names.dat', 'w') as n:
^
SyntaxError: invalid syntax
How do I fix this?

SyntaxError: Invalid Syntax in python when trying to use tarfile.open() on a .tgz file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Syntax error on print with Python 3
I want to view the contents of a .tgz file and I found python's tarfile module. I found the following tutorial which looked promising. http://www.doughellmann.com/PyMOTW/tarfile/
Here is my python file below:
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print tar.getnames()
When I actually execute my python file, I get a carrot sign pointing at the 'r' in the last line and the error message: SyntaxError: invalid syntax.
Print is function in python 3.x.
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print(tar.getnames())

Categories