import all functions vs. import specific function [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
In Python,
import shutil, os
allows me to call os.environ, os.path.exists(folder), os.listdir(pool), shutil.rmtree(folder) and shutil.copyree(). It seems that I call any function, defined in those modules. Nevertheless, I cannot call ctime() once I have imported import time. I must import ctime explicitly, by
from time import ctime
Why is such inconsistency? I find it difficult to program in such unpredictability.

from time import ctime allows to call ctime() directly, without time.ctime() prefix. It is me, who was inconsistent comparing fully qualified names os.listdir() with ctime() alone.

Related

What is causing the invalid syntax error in this very simple Python program? [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 12 months ago.
Improve this question
Just starting out learning Python and programming in general. Following along with a video on LIL and everything has worked fine up until now. I'm trying to access the first 3 rows of a specified series with the following code. I keep getting a syntax error where it highlights my colon. What am I doing wrong? It looks exactly like the text in the video.
import pandas as pd
import xlsxwriter
from openpyxl.workbook import Workbook
df = pd.read_excel("C:/Users/Aaron/Desktop/filename.xlsx")
print(df['Visit '],[1:3])
print(df['Visit '],[1:3])
The placement of the comma means you are passing two separate items to print():
df['Visit ']
[1:3]
The problem is the second item. [1:3] is not valid by itself.

Why is this simple Python script failing? Python 2.6.6 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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
Here's the script
#!/usr/bin/python
class LEG(Structure):
_fields_ = [("distance_sm", c_float), ("distance_nm", c_float)]
Here's what I get when I run it.
Traceback (most recent call last):
File "./test.py", line 3, in <module>
class LEG(Structure):
NameError: name 'Structure' is not defined
Yes, brand new to python, thanks for your help
It looks like you're trying to use the Structure type from the ctypes module. You need to import that, as well as c_float and anything else you use from that module:
from ctypes import Structure, c_float

Glob returns back an empty list in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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
My code:
hi = glob.glob("hi/FEC[0-9][0-9][0-9][0-9][0-9]_[0-9].seq")
I'm using a glob module and I'm trying to get all the path file names but all it returns back is a empty list. I don't know why it keeps doing that. It worked before my code was accidentally erased.
[]
Regardless if the file isn't even there, it still gives me an empty list.
Update:
So the following files I have are
FEC00001_1.seq
FEC00002_1.seq
FEC00002_2.seq
and so on..
Update 2:
So it I just realized that it might have to do something with the wrong folder like you guys said.
Because right now, my script is in "folder1" and all the files I'm trying to access is in "folder2"
/mainfolder/folder1/script.py
/mainfolder/folder2/files im trying to access
glob.glob is done by using the os.listdir() and fnmatch.fnmatch() functions in concert. So it's important that you search at the right place.
Considering your file hierarchy:-
/mainfolder/folder1/script.py
/mainfolder/folder2/files im trying to access
try the following :-
hi = glob.glob("../folder2/FEC[0-9][0-9][0-9][0-9][0-9]_[0-9].seq")

How to see the assembly code of a Python 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 8 years ago.
Improve this question
Out of curiosity I would like to see the assembly instructions that correspond to the code of a .py file. Are there any trustworthy solutions you can propose?
The dis module disassembles code objects (extracting those from functions, classes and objects with a __dict__ namespace).
This means you can use it to disassemble whole modules:
import dis
dis.dis(dis)
although this isn't nearly as interesting as you may think as most modules contain several functions and classes, leading to a lot of output.
I usually focus on smaller functions with specific aspects I am interested in; like what bytecode is generated for a chained comparison:
def f(x):
return 1 < x ** 2 < 100
dis.dis(f)
for example.

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