Search a CSV-file with python [duplicate] - python

This question already has answers here:
How do I check whether a file exists without exceptions?
(40 answers)
Closed 4 years ago.
I want to search a CSV-File with python2 on the Raspberrry Pi. If the file is not found the program should generate it. How I can search a file and can decide with an if-statment if there is CSV-file or not?

as #John Anderson said you can use the os module's os.path.
if os.path.exists('path/to/your/csvfile/') is True:
print("Do something")

Related

How can I find full path for a file? [duplicate]

This question already has answers here:
How to get an absolute file path in Python
(11 answers)
Closed 2 years ago.
Let's say I have this path
D:\something\something1\from_here_I_now\stuff\stuff2.
So, I know that \from_here_I_now\stuff\stuff2 is a permanent path, but the beginning is different, like I know that D:\something\something1\ may be different for someone else. How can I find the D:\something\something1\ knowing only \from_here_I_now\stuff\stuff2?
Try something like this:
import os
filestr = '\from_here_I_now\stuff\stuff2'
fullstr = os.path.abspath(filestr)
print(fullstr)
>>> 'D:\something\something1\from_here_I_now\stuff\stuff2'
print(fullstr[:len(filestr)])
>>> 'D:\something\something1'

Manipulate variables in Python [duplicate]

This question already has answers here:
How to get only the last part of a path in Python?
(10 answers)
Closed 5 years ago.
Is it possible to manipulate a variable, for example:
file = "/Python/work.txt"
to just list work.txt, without /Python? excluding everything on the left of the "/"?
Thank you
Of course! Simply do this:
file = "/Python/work.txt"
excluded = file.split("/")[-1]
This would return "work.txt" in the excluded variable.

use os.system('MyCommand') in the next command [duplicate]

This question already has answers here:
Equivalent of Bash Backticks in Python [duplicate]
(11 answers)
Closed 8 years ago.
As far as you know, we can use OS console commands, (For example dir,time and format in Windows) in Python programing using os.system('TheCommand') module. But this function return the state of Operation (0 for successful and 1 for failed).
I want to know if is there any way to use the output of the commands in the next commands? I mean (For example) I run os.system('dir') and save the list of directories in a variable!
This is fairly easy to do. Here I define the working directory and the edit time of a file as variables which I used later in my script.
#!/usr/bin/env python
PWD = os.getcwd()
edit_time=os.path.getmtime(file.txt)

How do you format for thousands separator in Python? [duplicate]

This question already has answers here:
Add 'decimal-mark' thousands separators to a number
(9 answers)
Closed 8 years ago.
I am simply trying to execute the following command in Python:
print("Number is", format(49000.12345,"10.2f"))
So I would like it to print out like 49,000.12 for the number.
My teacher taught us to put a comma in "10.2f" like ",10.2f" for the thousands separator but it's not working. Could someone please tell me the correct simple way similar to that?
Thank you
See this: http://www.python.org/dev/peps/pep-0378/ It is the PEP introducing the ability into Python 2.7, 3.1 and later versions.

How to get self file name [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
python, path of script
Is there any way that i can print the self filename of a file and current location.
Could __file__ be what you are looking for?

Categories