Python change dir in windows [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I cannot understand, how to read file in different dir on Windows.
>>> import os
>>> os.getcwd()
'C:\\Users\\vasyl.v\\AppData\\Local\\Programs\\Python\\Python37'
>>> Fh = open(“d:\\python\\monitor.py”, “r”)
SyntaxError: invalid character in identifier
Can anyone explain me, how to handle Windows paths in Python 3.7.x?

Try this
with open("d:\\python\\monitor.py", "r") as infile:
# do stuff with file here
data = infile.readlines()
in the code above I used what is called a "context manager", this will automatically close the file when the operations are finished. If the code above does not read your file, then either the path is incorrect, file does not exist, or you dont have proper permissions to read the file.

Related

os.path.join to create filename in python with datetime not working in mac [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to generate a filename in python on mac to record data everyday so that the filename has date in filename. Please refer the command below.
oi_filename= os.path.join("markets","storage","oi_data_records_{0}.json".format(datetime.now().strftime(%d%m%Y)))
where markets is in Desktop and storage is in markets folder.
Error
File "<ipython-input-20-e3a1aee3f506>", line 21
oi_filename= os.path.join("markets","storage","oi_data_records_{0}.json".format(datetime.now().strftime(%d%m%y)))
^
SyntaxError: invalid syntax
The basic idea is everyday a file to be created with full date in name so that the rest of program can park the data in the respective file.
It's because you didn't put the %d%m%y in a string.
oi_filename= os.path.join("markets","storage","oi_data_records_{0}.json".format(datetime.now().strftime(%d%m%Y)))
should be:
oi_filename= os.path.join("markets","storage","oi_data_records_{0}.json".format(datetime.now().strftime("%d%m%Y")))
You are missing quotation marks for strftime(%d%m%y). It should be strftime('%d%m%y')

Why am I getting a invalid syntax on this call method? Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to make a script that can delete a file with Python. When I moved the file to my startup directory with my script, a lot of the code was changed, not by me. I've just assumed this was normal and continued trying to make it delete a file on startup. I later realized it wasn't working because one of the call methods kept getting an invalid syntax. Here's my code.
Python Version: 3.8.7
Error message is :
invalid syntax (<unknown>, line 7)
LOAD_CONST(0), LOAD_CONST(None), IMPORT_NAME(os), STORE_NAME(os)
LOAD_CONST(0), LOAD_CONST(None), IMPORT_NAME(shutil), STORE_NAME(shutil)
source = 'C:\\Users\\me\\OneDrive\\Desktop\\startup.py'
destination = 'C:\\Users\\me\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup'
LOAD_NAME(shutil), LOAD_METHOD(move), LOAD_NAME(source), LOAD_NAME(destination), CALL_METHOD[2], STORE_NAME(new_path)
print(new_path)
LOAD_NAME(os), LOAD_METHOD(remove), LOAD_CONST('C:/Users/me/OneDrive/Desktop/delete.txt'), CALL_METHOD[1], POP_TOP
print('File Removed!'), return None
From the code, it's difficult to understand what you are trying to do. If you want to delete a file, you can use the os module in python. For example:
import os
os.remove("/some/file/path/to/remove.txt")

Reading Excel files using python3 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to read excel file using python to do some data analysis. The data file is located in the same folder as the python program. However, the code is giving a syntax error. Do not know why. Appreciate your help.
import pandas as pd
dataIn_df = pd.read_excel(r 'C:\Users\Jon\AppData\Local\Programs\Python\data\InputData.xlsx', sheet_name='InputData')
File "C:\Users\Jon\AppData\Local\Programs\Python\data\RateRegulator.py", line 54
dataIn_df = pd.read_excel(r 'C:\Users\Jon\AppData\Local\Programs\Python\data\InputData.xlsx', sheet_name='InputData')
^
SyntaxError: invalid syntax
There is an extra space between r and 'C:\... this is causing the SyntaxError.
r 'C:\Users\Jon\AppData\Local\Programs\Python\data\InputData.xlsx'
should be
r'C:\Users\Jon\AppData\Local\Programs\Python\data\InputData.xlsx'

ASCII files get erased after opening in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I started learning Python recently and came across of the following problem
I open a ASCII fine for reading, say data.txt and try to print it to the screen. I am using this code:
f = open("E:\ASCII\data.txt", "r")
for line in f:
print(repr(line))
This shows nothing on the screen, and the file gets erased of all the imformation contained, and gets 0kb of size.
I'm using Python 2.7.9 64-bit on Pycharm.
Thanks for all the help!
Not sure about the deletion, but this should print what you want
with open('E:\ASCII\data.txt', 'r') as f:
for line in f.read().splitlines():
print repr(line)

How to find specific files in a folder using python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
There are different files in a folder, I would like to print files which are ended with IHDRT.exr. The correct answer for this job is as shown bellow:
#!/usr/bin/env python
import glob
for file in glob.glob("*.exr"):
if file.endswith('iHDRT.exr'):
print(file)
#!/usr/bin/env python
import glob
for file in glob.glob("*.exr"):
if file.endswith('iHDRT.exr'):
^^^^^^^^
print(file)
Its endswith and not endswidth
Use endswith, not endswidth! Error spelling

Categories