ASCII files get erased after opening in Python [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 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)

Related

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'

Python change dir in windows [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 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.

Basic Python - print from for loop [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 5 years ago.
Improve this question
I created this list
bikes = ['trek','redline','giant']
and now am executing this simple for loop
for bike in bikes:
print(bike)
why is this my output?
>>> for bike in bikes:
... print(bike)
...
I expected the console to print out each value from the list.
Please help!
To run a file you must:
Write out your code in a text editor.
Save the file with the extension ".py" eg. loopingThroughAList.py
Run the file from the command-line or if in IDLE select Run > Run modulue.
The command for Windows is
python -3 loopingThroughAList.py
You must also be in the same directory as the file to run it with the command above.
Hope this helped!
EDIT: You are facing this problem because you haven't ended your for loop. When running code from the console you need to enter twice to end loops and functions.

How to write and save to a document in Python 3.4? [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've been trying to make a simple program to save user data (such as a level or stat) and so far nothing I've found online works.
test = open("sketch_pad.py","w")
test.write("This is a test\nOr is it?")
test.close
This brings up an error, along the lines of "Not writable file" (despite the fact that it is a .py)
Either this, or it will erase all the data in the document "sketch_pad.py", and not write anything.
Close method should be called (f.close()). To avoid these kind of mistakes and ensure releasing resources on errors/exceptions you may consider using Python with statement, e.g.
with open("file.txt","w") as f:
f.write("data")
close is a method and should be called as such; f.close() is the correct syntax, currently you are omitting the brackets ().
test = open("sketch_pad.py","w")
test.write("This is a test\nOr is it?")
test.close() # you need these brackets
should work.
You can dodge these kinds of mistakes by using Python's with statement:
with open("myFile.txt", "w") as myFile:
myFile.write("data")
# no need for myFile.close(),
# it is called automatically.

Categories