Reading file from variable 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 8 years ago.
Improve this question
I am trying read the contents of file through a variable
file = '/home/ec2-user/abc.txt'
paths = open("file","r+")
print paths.read()
But this is not working. I need help; I don't want specify 'abc.txt' directly.

First, you need to remove the quotes around "file":
paths = open(file,"r+")
When you're calling something using the value of a variable, you shouldn't quote the variable.
Second, if you don't want to hard code the path to file, you'll need to ask the user for it:
file = raw_input("Please enter the full path to the file: ")

Related

Print String 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 3 months ago.
Improve this question
I tried running this basic python script to print something, and it doesn't seem to be executing properly.
name = "Tyler";
print{name};
I am getting this error:
File "C:\Users\tyler\main.py", line 2
print{name};
^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
I tried changing line 2 to:
print(...)
but it prints out
Ellipsis, not my name.
You don't need semicolons in Python
The issue was the use of curly braces. Call your variable name with print() like this:
name = "Tyler"
print(name)

Python - Why is the function not working when reading from file? [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 10 months ago.
Improve this question
This is the code i have used
file
I think you meant to read all the lines from the file and look at the last one. Like this:
try:
lines = myfile.readlines()
GetTopic = lines[-1].strip()
...
Your code read one line into lines and lines[-1] was a single character and GetTopic = print(lines[-1]) meant that GetTopic was None and so not equal to any of your literal strings.
readline() returns a string - the first line of the document.
readlines() returns an array, which is probably what you're expecting.
I prefer to use lines = [x.strip() for x in myfile.readlines()]
The result of assigning a print() is always None. Python isn't the command line.

sorting a list with alphanumeric characters as filenames within a subdirectory 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 3 years ago.
Improve this question
I am trying to work with files which needs to be used for downstream processing.
The files in the subdirectory structure and the filenames are somewhat like :
./resources/json
-pdfextract_1_pdf.json
-pdfextract_4_pdf.json
-pdfextract_3_pdf.json
-pdfextract_2_pdf.json
When I just try to sort just the files in the subdirectort ./resources/json. It works.
mylist = ['pdfextract_2_pdf.json','pdfextract_3_pdf.json','pdfextract_1_pdf.json']
mylist.sort()
This one seems to be not working. Eventhough everything looks similar (just a string)
['pf1109__r_td6831__425_a_b_c.pdf.page33.pdf_testing_xml.json', 'pf1109__r_td6831__425_a_b_c.pdf.page4.pdf_testing_xml.json', 'pf1109__r_td6831__425_a_b_c.pdf.page41.pdf_testing_xml.json']
sorted() works perfectly well in python3
>>> mylist = ['./resources/json\\pdfextract_2_pdf.json','./resources/json\\pdfextract_3_pdf.json','./resources/json\\pdfextract_1_pdf.json']
>>> print(sorted(mylist))
['./resources/json\\pdfextract_1_pdf.json', './resources/json\\pdfextract_2_pdf.json', './resources/json\\pdfextract_3_pdf.json']
for descending order
sorted(mylist , reverse = True)
and for ascending order just use
sorted(mylist , reverse = false)
or
sorted(mylist)

How to use the value of variable as a key in a dictionary 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 5 years ago.
Improve this question
I'm continuing learning python and I want to use the value of a variable as a key in a dictionary, and the input of the user as the value of that key, but what I'm doing is doing the opposite.
def scan_option(option_type):
option_dict={}
elected_type = option_type + '_option'
option_dict.update(elected_type= raw_input("Enter the {} Option: ".format(option_type)))
return option_dict
print scan_option("Hardrive")
In case user answered 8:
The output is: {'elected_type': '8'}
and I want it to be: {'Hardrive_option': '8'}
What do I need to change in the code to do that?
update takes a dictionary as its argument. The stuff you fed it was a symbol and a string; it converts that to a dictionary and then does the update.
The syntax you need here is a simple dictionary assignment:
option_dict[elected_type] = raw_input(...)
You might try simpler commands when you first write your code. Once those work properly, then you can go for the one-line, compound commands.

finding an amount of variable in a text file by python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a variable which its amount is changing every time. I like to know how can I find the line which contained the amount of my variable.
I am looking something like this:
but in this way it looks for the letter of "A"
how can I write a command which look for its amount("100")?
I tried this before
A = 100
with open ('my_file') as f:
for line in f :
if "A" in line:
print line
Putting quotes around something makes it a String. You want to actually reference the variable which contains your number, i.e. A instead of "A"
A = 100
with open('my_file') as f:
for line in f:
# str() converts an integer into a string for searching.
if str(A) in line:
print line

Categories

Resources